0

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");
                            }
                        }

1 Answers1

0

This is happening because of the exact same reason I have told you, in the earlier question of yours. You are missing a child. When using the following line of code:

ds.child("monthamt").setValue(roundOff(mvalue + amt) + "");

That value is added directly under summary. To be able to change/add value, you should add that key in your reference:

ds.child("-MJMXp5Obhsd68HMrWax").child("monthamt").setValue(roundOff(mvalue + amt) + "");

Now, the new value for the monthamt property will be written under the correct node.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Alright that works for the month of "10". But what it push a random new key for new month of "11" for example. – Nicholas Leong Oct 11 '20 at 15:08
  • Then store that key into a variable and use it whenever you need it. This **[answer](https://stackoverflow.com/questions/51787784/how-to-get-specific-pushedid-in-firebase/51788244)** can help you achieve that. Check also Frank van Puffelen's **[answer](https://stackoverflow.com/questions/52691457/accessing-firebase-database-objects-below-child)**. Give a try and tell me if it works. – Alex Mamo Oct 11 '20 at 15:12
  • Please also provide some feedback for my answer from this **[post](https://stackoverflow.com/questions/64299818/datasnapshot-could-not-get-parent-push-key-value)**. – Alex Mamo Oct 11 '20 at 15:15
  • I create another "key1" and store dataSnapshot.getKey(). It works when I match with the "month" value. But it will push another new key with the same month into Firebase. String key1 = dataSnapshot.getKey(); ds.child(key1).child("monthamt").setValue(roundOff(mvalue + amt) + ""); – Nicholas Leong Oct 11 '20 at 15:28
  • No, no, no. You should only push it once. You get the key, and you use it in your reference and that's it. But note, this is working when you set the value. When you read the value, you should use a query. – Alex Mamo Oct 11 '20 at 15:35
  • It can sum up the "monthamt" now if same "month" but it still will push a new key. I have no idea why :( – Nicholas Leong Oct 11 '20 at 16:28
  • Don't push a new key, simply reuse the existing one. – Alex Mamo Oct 11 '20 at 16:45
  • It will automatically push key as i declare String key = ds.push().getKey() in the beginning, I tried take out the .push on String key and added to those statement which "month" not equalTo and it appears that it could not push key anymore and store value direct under "summary" instead. – Nicholas Leong Oct 11 '20 at 16:53
  • I have update my complete code on the question for your more understanding. The problem code is at the ELSE statement of if(equal(month)) – Nicholas Leong Oct 11 '20 at 17:03
  • There is no need to attach the listener twice. Remove the second (addChildEventListener) and get the data out directly from the first `dataSnapshot` object. – Alex Mamo Oct 11 '20 at 17:35
  • If I did not attach (addChildEventListener), this code: String month = (String) dataSnapshot.child("month").getValue(); could not get the "month" value and getting empty value instead – Nicholas Leong Oct 11 '20 at 17:44
  • You are adding `addListenerForSingleValueEvent` and right after that `addChildEventListener`. There's no need for that. – Alex Mamo Oct 11 '20 at 17:57