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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…