count the nodes of a child in the firebase

萝らか妹 提交于 2021-01-29 06:10:13

问题


I have a structure like this in firebase:

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?


回答1:


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.

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


来源:https://stackoverflow.com/questions/53062882/count-the-nodes-of-a-child-in-the-firebase

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