简体   繁体   中英

Calculating child node values in firebase

I'm trying to add the quantity from each userid and display the total figure in a textview. 在此处输入图像描述

I have a sample code but it is not working

final  TextView Quant = (TextView)findViewById(R.id.Quant);
    DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("History");
    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            int sum = 0;
            for(DataSnapshot ds : dataSnapshot.getChildren())
            {
                Map<String,Object> map = (Map<String,Object>)ds.getValue();
                Object Quantity = map.get("Quantity");
                int pValue = Integer.parseInt(String.valueOf(Quantity));
                sum += pValue;
                Quant.setText(String.valueOf(sum));
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Change this:

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("History");

to this:

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("history");

history is lowercase in your database, should be the same here also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM