计算firebase中一个孩子的节点数 [英] count the nodes of a child in the firebase

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

问题描述

我在firebase中有这样的结构:

I have a structure like this in firebase:

在节点"postagens-curtidas"中,我需要对这个子节点进行计数,并将该值保存在"qtdCurtidas"节点中,如图所示.

Inside the node 'postagens-curtidas', I need to count this childrens and save this value at the 'qtdCurtidas' node, as the photo shows.

有没有办法做到这一点?

Is there a way to make this?

推荐答案

final long count;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("postagens-curtidas").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            count = dataSnapshot.getChildrenCount();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

这是获取子节点总数的方法.

This is how you get the total number of children nodes.

假设您具有保存qtdCurtidas的密钥.您可以将其保存如下

Assuming you have the key where qtdCurtidas is saved. You can save it as follows

ref.child("postagens-curtidas").child("Your Key Here").child("qtdCurtidas").setValue(count);

这篇关于计算firebase中一个孩子的节点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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