[英]How to count the specific values from firebase in android studio?
There are four seasons in the category, but I want to count only "Spring".该类别有四个季节,但我只想计算“春天”。 Finally, I want to print the counted number on the screen.
最后,我想在屏幕上打印计数的数字。 What should I do?
我应该怎么办?
If I understood correctly, you want to get the number of children that exist under the "Products" node, which has the field productSeasonCategory
set to "Spring".如果我理解正确,您想获取“Products”节点下存在的子节点数,该节点的
productSeasonCategory
字段设置为“Spring”。 If that's the case, then instead of using a database reference, you should use a query, as you can see in the following lines of code:如果是这种情况,那么您应该使用查询而不是使用数据库引用,如以下代码行所示:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference productsRef = db.child("Users").child(uid).child("Products");
Query queryByProductSeasonCategory = productsRef.orderByChild("productSeasonCategory").equalTo("Spring");
queryByProductSeasonCategory.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
long count = task.getResult().getChildrenCount();
Log.d("TAG", "count: " + count);
Text1.settext(String.valueOf(count)) //Added
} else {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.