如何从Firebase中的节点获取所有值的总和? [英] How to get a sum of all values from a node in Firebase?

查看:129
本文介绍了如何从Firebase中的节点获取所有值的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据库结构:

我想从所有子值中得到总和,例如我希望得到de值5,它是节点的所有de值之和UNICARIBE

I want to get the sum from all child values, from example for I want to get de value "5" that is the sum from all de values of the Node "UNICARIBE"

推荐答案

要计算所有这些值,请使用以下代码:

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

您的输出将是 5

这篇关于如何从Firebase中的节点获取所有值的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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