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". 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!
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.