How to get a sum of all values from a node in Firebase?

后端 未结 1 1542
难免孤独
难免孤独 2020-12-18 17:24

this is my Database structure:

\"JSON\"

I want to get the sum from all child values, from example for I

相关标签:
1条回答
  • 2020-12-18 17:42

    To count all those value, please use the following code:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference unicaribeRef = rootRef.child("Unicaribe");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            int total = 0;
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                int value = ds.getValue(Integer.class);
                total =+ value;
            }
            Log.d("TAG", String.valueOf(total));
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    unicaribeRef.addListenerForSingleValueEvent(eventListener);
    

    Your output will be 5.

    0 讨论(0)
提交回复
热议问题