如何从Firebase中求和多个值 [英] How to sum multiple values from firebase

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

问题描述

我还是android编程和firebase的新手.首先看图片

I'm new at android programming as well as firebase. first of all look at the picture

详细图片

在这里,我要获得总计(总和)中所有的成本"部分.我已经创建了这样的参考

In there I want to get all the "cost" part in total(sum). I've created the reference like that

DatabaseReference databaseBazars=firebaseDatabase.getInstance().getReference("BazarList");

现在我可以遵循什么程序来获取和并在列表视图或祝酒消息中显示它.

now what procedure I can follow to get sum and show it in list view or in a toast message).

我这样做是为了从那个(BazarList)节点获取所有数据.并完美地显示在listview上.

I've done this to get all the data from that(BazarList) node . and it perfectly showing on the listview.

  databaseBazars.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                 bazarList.clear();
                 for( DataSnapshot ds :dataSnapshot.getChildren()) {
                     Bazar bazar = ds.getValue(Bazar.class);
                     bazarList.add(bazar);
                 }
            BazarList adapter = new BazarList(Bazar_Data_Input.this,bazarList);
                listViewBazars.setAdapter(adapter);
            }


            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

现在,我如何获得费用的总和.任何人,请帮助我

now, how can I get the sum part of the cost. anyone, please help me

这是市场分类

public class Bazar {
    String bid;
    String date;
    String cost;
    String name;
    String item;

    public Bazar(){

    }

    public Bazar(String bid, String date, String cost, String name,String item) {
        this.bid = bid;
        this.date = date;
        this.cost = cost;
        this.name = name;
        this.item= item;
    }

    public String getBid() {
        return bid;
    }

    public String getDate() {
        return date;
    }

    public String getCost() {
        return cost;
    }

    public String getName() {
        return name;
    }

    public String getItem() {
        return item;
    }
}

推荐答案

要解决此问题,请使用以下代码:

To solve this, please use the following code:

databaseBazars.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        bazarList.clear();
        Integer total = 0;

        for( DataSnapshot ds :dataSnapshot.getChildren()) {
            Bazar bazar = ds.getValue(Bazar.class);
            Integer cost = Integer.valueOf(bazar.getCost());
            total = total + cost;
            bazarList.add(bazar);
        }
        BazarList adapter = new BazarList(Bazar_Data_Input.this,bazarList);
        listViewBazars.setAdapter(adapter);
        Log.d("TAG", total + "");
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
});

输出为:59512.

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

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