Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 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 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...