所以我有一个firebase数据库,如下所示:
xyz:
--x:
----m1:
------col1(string)
------col2(string)
------col3(int)
我有一个集成了 Razorpay API的android项目,我想在付款成功后增加col3的值。
这是 payment.class 文件:
package com.example.xxx.myapplication;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.razorpay.Checkout;
import com.razorpay.PaymentResultListener;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Payment extends AppCompatActivity implements PaymentResultListener {
EditText value;
Button pay;
int payamount;
FirebaseDatabase database;
DatabaseReference db;
int donation_amt;
@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("x");
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
List<x> m = new ArrayList<>();
for(DataSnapshot ds1:dataSnapshot.getChildren()){
x md = dataSnapshot.getValue(x.class);//getter and setter functions are stored in x.class
donation_amt = md.getContribution();//getter function getContribution() i.e col3
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
private void startPayment() {
payamount = Integer.parseInt(value.getText().toString()); //storing edittext value and converting into integer type
Checkout checkout = new Checkout();
final Activity activity = this;
try {
JSONObject options = new JSONObject();
options.put("currency", "INR");
options.put("amount", payamount*100);
checkout.open(activity, options);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onPaymentSuccess(String s) {
Toast.makeText(Payment.this, "Your payment is successful", Toast.LENGTH_SHORT).show();
donation_amt = donation_amt + payamount;
}
@Override
public void onPaymentError(int i, String s) {
Toast.makeText(Payment.this,"Your payment is unsuccessful",Toast.LENGTH_SHORT).show();
}
}
成功付款不会更新数据库中的值
请帮助。
答案 0 :(得分:1)
这是因为您没有使用int
变量来引用Firebase。您只是在本地增加它。这样的事情应该起作用
FirebaseDatabase db = FirebaseDatabase.getInstance();
final DatabaseReference ref = db.getReference().child("x");
@Override
public void onPaymentSuccess(String s) {
Toast.makeText(Payment.this, "Your payment is successful", Toast.LENGTH_SHORT).show();
donation_amt = donation_amt + payamount;
ref.child("m1").child("col3").setValue(donation_amt);
}