Reputation: 111
In my app there is an option for referrals, I am using the firebase database to update the wallet if referral code matched, but the problem is user account is credting successfully but agent account(who referred )is credting multiple times like over and over continuously and that's the issue,
public void checkreffercode() {
final String refferalcode = editText.getText().toString();
if (refferalcode.equals("")) {
Toast.makeText(ReferalActivity.this, "Refferal code is empty", Toast.LENGTH_LONG).show();
} else {
final DatabaseReference reffercoderef = FirebaseDatabase.getInstance().getReference().child("Reffercode");
final DatabaseReference agentwallet = FirebaseDatabase.getInstance().getReference();
final DatabaseReference userwalletstat = FirebaseDatabase.getInstance().getReference(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Refferal");
final DatabaseReference userwallet = FirebaseDatabase.getInstance().getReference(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("walletcoin");
userwalletstat.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String statous = dataSnapshot.getValue(String.class);
if (statous == null) {
Toast.makeText(ReferalActivity.this, "data is null in Refferal", Toast.LENGTH_LONG).show();
} else if (statous.equals("notcredited")) {
//here check all the feild....
reffercoderef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(refferalcode).exists()) {
final String Uid = dataSnapshot.child(refferalcode).getValue(String.class);
userwallet.setValue(String.valueOf(5000));
agentwallet.child(Uid).child("walletcoin").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String agentbalance = dataSnapshot.getValue(String.class);
Integer agentbalinint = Integer.parseInt(agentbalance);
Integer updatedbalu = (agentbalinint + 5000);
agentwallet.child(Uid).child("walletcoin").setValue(String.valueOf(updatedbalu));
userwalletstat.setValue("credited");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(ReferalActivity.this, "error on updating agent balance", Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(ReferalActivity.this, "Refferal code not found", Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(ReferalActivity.this, "Database error on checking refferal code", Toast.LENGTH_LONG).show();
}
});
//last
} else {
Toast.makeText(ReferalActivity.this, "Both wallet has been credited", Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(ReferalActivity.this, "Database error on checking account status", Toast.LENGTH_LONG).show();
}
});
}
}
public void updateref(View view) {
Button button=findViewById(R.id.refbutton);
final ProgressDialog progressDialog = new ProgressDialog(ReferalActivity.this);
progressDialog.setMessage("Please wait..");
progressDialog.getActionBar();
progressDialog.setCancelable(false);
progressDialog.show();
checkreffercode();
progressDialog.cancel();
}
Here is my xml code
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Refer code"
android:id="@+id/refercode"
android:inputType="textPersonName"
android:padding="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:onClick="updateref"
android:layout_gravity="center"
android:id="@+id/refbutton"
android:textColor="#51122e"/>
Upvotes: 0
Views: 615
Reputation: 1322
You are using ValueEventListener
on userwalletstat
reference. The problem is that with ValueEventListener
is that, it gets called every time a value changes under userwalletstat
("referal") child. Inside your else if (statous.equals("notcredited"))
condition, you are using another ValueEventListener
in which you are setting setting the value like this userwallet.setValue(String.valueOf(5000));
So your first ValueEventListener gets activated because it's a ValueEventListener
. So your code is kind of like a recusive function and it is bad. So in order to fix it, you should replace your ValueEventListeners
with SingleValueEventListener
. You can learn more about them here and scroll down to Read Data Once section.
Basically, you need to replace
userwalletstat.addValueEventListener(new ValueEventListener() {
with
userwalletstat.addListenerForSingleValueEvent(new ValueEventListener() {
and do the same for other listeners like :
reffercoderef.addListenerForSingleValueEvent(new ValueEventListener() {
agentwallet.child(Uid).child("walletcoin").addListenerForSingleValueEvent(new ValueEventListener() {
Also, I don't know how your data structure is setup but it is not a good idea to have a listener inside a listener inside a listener. Maybe you should reorganize your datastructure.
Upvotes: 1