1

enter image description here

prev.child("ViewLikes").child(postId).push().setValue("viewed");

How to count the number of childs under the the root(9999999977). I'm using Value event listener but still the I can't get the counts.

Inside Value event listener below for loop is used.

for(DatanSnapshot a:DatanSnapshot.getChildren())
long count=a.getChildrenCount();
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • 2
    Possible duplicate of [Firebase Android count children/ badge](https://stackoverflow.com/questions/37600442/firebase-android-count-children-badge) – AskNilesh Mar 29 '18 at 06:42
  • https://stackoverflow.com/questions/43606235/android-firebase-get-childrens-count – AskNilesh Mar 29 '18 at 06:43

1 Answers1

1

To get the number of childrens within the 9999999977 node, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("ViewLikes").child("9999999977");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        long count = dataSnapshot.getChildrenCount();
        Log.d("TAG", "count= " + count);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(valueEventListener);

The out will be: count= 7

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193