1

I have one value at database child"received" field. I need to update that with a new value by some Integer addition program. Actually I am trying to add that value through one AlertDialog, In that, I want to add the currentValue present at database with my entered value. But its keep adding those values whenever the database value changing. I couldn't be able to post the code outside the database operation. How I can achieve that?

My code

amountEnteredString=mAmount.getText().toString();

                DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(userName);
                final DatabaseReference receivedRef = ref.child(month).child(customerName).child("received");

                receivedRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        currentDatabaseValueString = dataSnapshot.getValue(String.class);

                        Integer currentDatabaseValue=Integer.parseInt(currentDatabaseValueString);

                        Integer amountEntered=Integer.parseInt(amountEnteredString);

                        Integer result=currentDatabaseValue+amountEntered;

                        String resultString=Integer.toString(result);

                        receivedRef.setValue(resultString);

                        dialoag.dismiss();

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                        Log.w("name", "load:onCancelled", databaseError.toException());

                    }

                });

            }
        });

        mBuilder.setView(mView);
        dialoag= mBuilder.create();
        dialoag.show();

    }
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Dipin K G
  • 109
  • 1
  • 11

0 Answers0