D
D
Danil2019-05-19 15:34:08
Java
Danil, 2019-05-19 15:34:08

How to summarize firebase data in android?

Good afternoon! Essence of the question.
I have a field in Firebase, - balance.
What I need - after the person enters the amount - 100, the field changes the value to 100, then the person enters the amount 50, the field value becomes 150.
How can I write the processing logic on the client? Data summation.
I think this is oversimplistic, but I need your help!

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



                String user_id = mAuth.getCurrentUser().getUid();
                
                String balance = dataSnapshot.child(user_id).child("Balance").getValue(String.class);

                mCountPayment.setText(balance + " ₽");

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        mPaymentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                paymentIntent();



            }
        });
    }

    private void paymentIntent() {


        final String user_id = mAuth.getCurrentUser().getUid();

        final String count = mPaymentList.getText().toString().trim();

        if (!TextUtils.isEmpty(count)) {


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


                    mDatabaseUsers.child(user_id).child("Balance").setValue(count);


                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });


        }


    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
orbit070, 2019-05-19
@orbit070

Two ways:
1. You read the previous value from firebase in advance, add the numbers on the client, and the result of the sum is already overwritten.
2. Look towards transactions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question