'how to update value of a firebase column on event success

I am using the RazorPay API in my android app and I want to update the value of a particular user's "count" on successful payment. So I put the following code for the updation of the count in the onSucessfulPayment() method but even on the account of successful payment, the value of count is not getting updated.

My database is like:

Userdata:

___user1

_____name

_____age

_____count

___user2

Here's the code:

public class Payment extends AppCompatActivity implements PaymentResultListener {

    EditText value;
    Button pay;
    int payamount;
    FirebaseDatabase database;
    DatabaseReference db;
    int d;
    String da;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment);

        value = findViewById(R.id.input);
        pay = findViewById(R.id.razorpay);

        pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //define method for payment process
                startPayment();
            }
        });
        db = FirebaseDatabase.getInstance().getReference().child("db");
        SharedPreferences sharedPreference;
        users u = new users();
        db.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                List<users> u = new ArrayList<>();
                for(DataSnapshot ds1:dataSnapshot.getChildren()){
                    users md = dataSnapshot.getValue(users.class);
                    d = md.getCount();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                    throw databaseError.toException();
            }
        });

    }

    private void startPayment() {
        pa = Integer.parseInt(value.getText().toString());         Checkout checkout = new Checkout();
        final Activity activity = this;
        try {
            JSONObject options = new JSONObject();
           
            options.put("amount", payamount*100);
            checkout.open(activity, options);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onPaymentSuccess(String s) {

        try {
            Toast.makeText(Payment.this, "Your payment is successful", Toast.LENGTH_SHORT).show();
            da = da + pa;
            da = Integer.toString(da);
            FirebaseAuth fa;
            FirebaseUser u;
            fa = FirebaseAuth.getInstance();
            u = fa.getCurrentUser();
            String uid = u.getUid();
            db.child(u.getUid()).child("count").setValue(da);
        }
        catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void onPaymentError(int i, String s) {
        Toast.makeText(Payment.this,"Your payment is unsuccessful",Toast.LENGTH_SHORT).show();
    }
}


Solution 1:[1]

You Might want to add a Success listener in order to check if a write is successful, you also can use a transaction if its key to your business logic

    db.child(u.getUid()).child("count").setValue(da)  .addOnSuccessListener(new OnSuccessListener<Void>() {
    @Override
    public void onSuccess(Void aVoid) {
        // Write was successful!
        // ...
    }
})
.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        // Write failed
        // ...
    }
});

OR Make it a transaction by calling start transaction on "count" reference

Solution 2:[2]

Just add an onSuccessListener as in the other answers. You don't need onFailureListener as the code will always be stuck there because there is no exception. Firebase is not throwing an error. It will just try to update the data whenever an Internet connection will be available.

Rather you can combine CheckNetworkState with this to avoid errors.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mahi Tej Gvp
Solution 2 Rachel Gallen