在firebase中运行两个查询

时间:2017-03-14 09:25:30

标签: android firebase-realtime-database

我有以下代码,我试图计算销售和支出之间的差异。有没有办法找到dref和dreff的结果之间的差异???

dref= dref.child("KatakaExpenditure");
dreff= dreff.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");**

我想要做的是获取数据库中两个孩子总数的差异...我意识到这两个变量不能在他们的方法之外访问。

0 个答案:

没有答案