Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

android - Calculate the difference between two integers in firebase

I have the following code, i was trying to calculate the difference between sales and expenditure..I have to query the database differently in order to achieve this. I am looking for a simple way to do this or a better way to achieve the same with the code below:

dref= dref.child("KatakaExpenditure");
dreff= dref.child("KatakaSales");

        dref.addValueEventListener(new ValueEventListener() {


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


                textView4.setText("Your total expenses for Kataka Unit is:" +Integer.toString(totalAmount));
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }


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

        dreff.addValueEventListener(new ValueEventListener() {


            @Override
            public void onDataChange(DataSnapshot snapshot) {
                int totalAmountt = 0;
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    DogExpenditure dogExpenditure = postSnapshot.getValue(DogExpenditure.class);
                    totalAmountt += dogExpenditure.getAmount();
                }


                textView4.setText("Your total expenses for Kataka Unit is:" +Integer.toString(totalAmountt));
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }


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


        textView4.setText("Your total expenses for Kataka Unit is:" +Integer.toString(totalAmountt));

int profit = totalAmountt - totalAmount;
Where totalAmount is the **dref= dref.child("KatakaExpenditure");
** and totalAmountt is **dreff= dref.child("KatakaSales");**
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...