如何在Firebase中检索根节点下的孩子数 [英] How to Retrieve Count of Child Under Root Node in Firebase

查看:49
本文介绍了如何在Firebase中检索根节点下的孩子数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如何计算根目录下的子节点数(9999999977).我正在使用Value事件侦听器,但仍然无法计数.

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.

使用下面for循环的内部值事件侦听器.

Inside Value event listener below for loop is used.

for(DatanSnapshot a:DatanSnapshot.getChildren())
long count=a.getChildrenCount();

推荐答案

要获取 9999999977 节点内的子代数,请使用以下代码:

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

结果将是: count = 7

这篇关于如何在Firebase中检索根节点下的孩子数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆