我只想从我的 firebase 数据库中获取 bool 数据,然后计算其中有多少 bool 数据。
这是我数据库中的 json 树。
"user_study_condition" : {
"1lYNWnabn7a6mj9cPOcayHOGUjx2" : {
"algorithm" : {
"bubble" : true,
"insert" : false,
"name" : "알고리즘"
},
"data_structure" : {
"name" : "자료구조",
"queue" : true,
"stack" : false
}
}
我只想统计真实数据。在这棵树中,我将统计 2 个真实数据。然后添加到列表中。我已经知道如何从数据库中检索数据,例如“addSingleValueListener...addValueListener..”
但我不知道如何计算 bool 数据量。
我必须动态计算 bool 数据。因为主题将被动态创建。和 bool 数据会经常改变。所以我想知道考虑到这一点的解决方案。
感谢您的回复。
最佳答案
你可以通过使用 equalTo 来做到这一点
myRef.orderByChild(childName).equalTo(true);
然后在你的监听器上你可以得到计数
dataSnapShot.getChildrenCount()
希望对你有帮助
编辑:
试试这个动态键。首先是获取基本引用。示例
myRef = ref.child("1lYNWnabn7a6mj9cPOcayHOGUjx2")
然后添加监听器,然后在监听器中执行此操作
for (DataSnapshot snapShot: dataSnapshot.getChildren()) {
Map<String, Object> mp= (Map<String, Object>) snapShot.getValue();
//First is to check if it contains true
if(mp.containsValue(true)){
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
//If object is a boolean check if it is true
if(pair.getValue() instanceof Boolean){
if(pair.getValue()){
//This mean that we have 1 true
ctrTrue++;
//This refers to the child reference
//For example "1lYNWnabn7a6mj9cPOcayHOGUjx2/algorithm"
DatabaseReference itemRef = myRef.child(pair.getKey())
}
}
it.remove();
}
}
}
关于Android,Firebase 如何只从数据库中获取 bool 数据并计算它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50092842/