问题
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