1

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: Wrong counting in borrowed book portion

This picture shows my database in firebase: Database of 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) {

        }
    });
Excalibur
  • 21
  • 5
  • That's because the `DataSnapshot` returned in the `onChildAdded` method contains the recently added child. Is there any specific reason why you need to use a `ChildEventListener` instead of a `ValueEventListener`? – Rosário Pereira Fernandes Feb 09 '18 at 19:33
  • Every time I open an activity there's a delay when retrieving the data. – Excalibur Feb 10 '18 at 06:50
  • The delay has to do with the network conditions. This same delay might happen when using the ChildEventListener, so I don't think you should bother about it. I recommend using a Single ValueEventListener – Rosário Pereira Fernandes Feb 10 '18 at 12:07

2 Answers2

1

Child listeners are triggered once for each child found at the reference you choose, and continually over time as children are added, changed and removed. It will only stop triggering those events when the listener is removed from the reference. It is not a one-time query like a single value event listener.

If you want to know the number of children at a location in a single query, addListenerForSingleValueEvent() on the reference. That will yield a DataSnapshot whose children you can iterate and count.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

databaseReference.child("Mark as Borrowed").addChildEventListener(new ChildEventListener // then check the uid of the value from snapshot to match your desired uid before update the TextView. This should do the trick, but very inefficient since it will read other users too. As Rosário Pereira Fernandes suggested, what's wrong with ValueEventListener? If you want to deal with each book separately, add a instance variable, each time onChildAdded is called, increase that variable by 1 and do the handling logic.

Son Nguyen
  • 1,124
  • 2
  • 10
  • 24