How to Retrieve Count of Child Under Root Node in Firebase

老子叫甜甜 提交于 2021-01-28 07:37:41

问题


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();

回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!