Skip to content Skip to sidebar Skip to footer

How To Retrieve Count Of Child Under Root Node In Firebase

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

Solution 1:

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

DatabaseReferencerootRef= FirebaseDatabase.getInstance().getReference();
DatabaseReferenceref= rootRef.child("ViewLikes").child("9999999977");
ValueEventListenervalueEventListener=newValueEventListener() {
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        longcount= dataSnapshot.getChildrenCount();
        Log.d("TAG", "count= " + count);
    }

    @OverridepublicvoidonCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(valueEventListener);

The out will be: count= 7

Post a Comment for "How To Retrieve Count Of Child Under Root Node In Firebase"