I want to sum up the "monthamt" inside a key if "month" is equal to the month on Firebase. When I run this code, it appears to be: Firebase for Summary
I have update my complete code as below.
The code to sum up the "monthamt", I have no idea how to direct it to the key:
final String key = ds.push().getKey();
ds.orderByKey().addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
ds.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Calendar c = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("ddMMyy");
SimpleDateFormat fm = new SimpleDateFormat("MM");
String month = (String) dataSnapshot.child("month").getValue();
//testing
double value = Double.valueOf(String.valueOf(dataSnapshot.child("amt").getValue()));
double mvalue = Double.valueOf(String.valueOf(dataSnapshot.child("monthamt").getValue()));
value = roundOff(value);
mvalue = roundOff(mvalue);
if (fm.format(c.getTime()).equals(month)) {
String key1 = dataSnapshot.getKey();
ds.child("-MJMXp5Obhsd68HMrWax").child("monthamt").setValue(roundOff(mvalue + amt) + "");
Toast.makeText(newTransaction.this, "ok", Toast.LENGTH_SHORT).show();
} else {
Calendar b = Calendar.getInstance();
ds.child(key).child("date").setValue(format.format(b.getTime()));
ds.child(key).child("amt").setValue(amt + "");
ds.child(key).child("month").setValue(fm.format(b.getTime()));
ds.child(key).child("monthamt").setValue(amt + "");
ds.child(key).child("trans").setValue("1");
dt.child("Bill").child(k).child("gtranid").setValue("1");
Toast.makeText(newTransaction.this,month, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
} else {
Calendar c = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("ddMMyy");
SimpleDateFormat fm = new SimpleDateFormat("MM");
ds.child(key).child("date").setValue(format.format(c.getTime()));
ds.child(key).child("amt").setValue(amt + "");
ds.child(key).child("month").setValue(fm.format(c.getTime()));
ds.child(key).child("monthamt").setValue(amt + "");
ds.child(key).child("trans").setValue("1");
dt.child("Bill").child(k).child("gtranid").setValue("1");
}
}