Please how do I simply get the child count from a firebase Query. For example Let\'s say I use a database query with 10 children, how do I get that value because I tried using <
The problem was this line:
databaseReference.child("PrinterView").child(uni).child(phone).orderByChild("done").equalTo("No").addValueEventListener(new ValueEventListener()
The variable "uni" and "phone" was also provided by a firebase Query E.G It was to get the University and phone number of the current user so I could not just put a static string there.
databaseReference.child("Users").child(mAuth.getCurrentUser().getUid()).child("Phone").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
phone = dataSnapshot.getValue().toString();
// Toast.makeText(getActivity(), phone, Toast.LENGTH_LONG).show();
setPendingList();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
So the issue was that, if the network connection was slow or if the queries were not done yet, those two variables would be empty thereby making the dataSnapshot empty. I will have to sort that out by only allowing that to be queried when the rest are sure to be done. :)
Assuming that PrinterView
is a direct child of your Firebase root, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("PrinterView").child("Covenant University").child("588");
Query query = yourRef.orderByChild("done").equalTo("No");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
long count = dataSnapshot.getChildrenCount();
Log.d("TAG", String.valueOf(count));
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
query.addListenerForSingleValueEvent(eventListener);