问题
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();
回答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
来源:https://stackoverflow.com/questions/49549582/how-to-retrieve-count-of-child-under-root-node-in-firebase