My problem is how can I count child nodes in addChildEventListener? The code below work on addValueEventListener and It counts my child correctly but when I use addChildEventListener. It doesn't work. The reason why I used this, is because of this thread: Firebase Android count children/ badge. I tried this but It doesn't work somehow in my code.
In this picture, the borrowed books should count as 2 but it returns to 10:
This picture shows my database in firebase: . The reason why it returns to 10 because it counts the children of the child of
user.getUid()
. All I want to count is the children of user.getUid()
This is the portion of my code:
databaseReference.child("Mark as Borrowed").child(uid).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String number = dataSnapshot.getChildrenCount()+"";
btnNumber.setText(number);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});