Exchange

Sum in Firebase Real-time database children - Android Sudio

 


private void countAndSum() {
FirebaseDatabase.getInstance().getReference().child("Hospital").child("Users").child(uid).child("Cart")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {

int sum = 0;

if(snapshot.exists()){
for(DataSnapshot ds : snapshot.getChildren()){
Map<String, Object> map = (Map<String, Object>) ds.getValue();
Object amount = map.get("Amount");
int pValue = Integer.parseInt(String.valueOf(amount));
sum += pValue;

tTotalItemsCost.setText("Rs." + String.valueOf(sum));
}
}
}

@Override
public void onCancelled(@NonNull DatabaseError error) {

}
});
}



Post a Comment

0 Comments