简体   繁体   中英

Calculating the sum of Firebase records values

I have a database structure like this one here.

在此处输入图片说明

My objective is to get the total of the two amounts 12000 and 15000 and that would be 27000..However the code i have tried to use is giving me a value like null1200015000 . Here is the code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mystatement);
        listview = (ListView) findViewById(R.id.listview);

        textView4 = (TextView) findViewById(R.id.textView4);


        dref = FirebaseDatabase.getInstance().getReference();


        DatabaseReference dref = FirebaseDatabase.getInstance().getReference();
        dref = dref.child("Expenditure");

        dref.addValueEventListener(new ValueEventListener() {


            @Override
            public void onDataChange(DataSnapshot snapshot) {
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    //Getting the data from snapshot
                    DogExpenditure dogExpenditure = postSnapshot.getValue(DogExpenditure.class);

                    //Adding it to a stringString expenses = "Amount: "+dogExpenditure.getAmount()+"\nReason for Use: "+dogExpenditure.getItem()+"\n\n";

                    String amount =  dogExpenditure.getAmount();


                    textView4.setText(amount);
                }


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }


            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());
            }
        });

Anyone with an idea on how to achieve this??? Here is my DogExpenditure class:

public class DogExpenditure {
    private String amount;
    private String item;

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }
}

Define DogExpenditure dogExpenditure globally and Use this

     dref.addValueEventListener(new ValueEventListener() {


        @Override
        public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                //Getting the data from snapshot
                DogExpenditure dogExpenditure = postSnapshot.getValue(DogExpenditure.class);

                //Adding it to a stringString expenses = "Amount: "+dogExpenditure.getAmount()+"\nReason for Use: "+dogExpenditure.getItem()+"\n\n";


            }

         if(dogExpenditure.size()>0){

             int amt=0;
            for(int i=0;i<dogExpenditure.size();i++){

                 amt =amt+Integet.parseInt(dogExpenditure.get(i).getAmount())

              }

          textView4.setText(""+amt);
          }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }


        public void onCancelled(FirebaseError firebaseError) {
            System.out.println("The read failed: " + firebaseError.getMessage());
        }
    });

If I understand it correctly it should be quite simple:

First, change amount in DogExpenditure into double or int :

public class DogExpenditure {
    private int amount;
    ...
    public int getAmount() {
        return amount;
    }
    ...
}

Then inside your onDataChange() just increment it by each amount value. Like this:

public void onDataChange(DataSnapshot snapshot) {
    int totalAmount = 0;
    for (DataSnapshot postSnapshot : snapshot.getChildren()) {
        DogExpenditure dogExpenditure = postSnapshot.getValue(DogExpenditure.class);
        totalAmount += dogExpenditure.getAmount();
    }
    textView4.setText(totalAmount.toString());
}

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