I am using Firestore adapter for my RecyclerView
and I am having trouble with the 'Delete' button. When I press it, it deletes the wrong item instead of the one that I wanted.
Here is the code for my button inside of the onBindViewHolder
:
protected void onBindViewHolder(@NonNull AdminRewardAdapter.RewardViewHolder holder, int position, @NonNull RewardModel model) {
fStore = FirebaseFirestore.getInstance();
holder.rank.setText(String.valueOf(position + 1));
Double dq = model.getDonationReq();
holder.donationRequired.setText(String.format("%.0f", dq));
holder.rewardDescription.setText(model.getRewardDesc());
holder.delete_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CollectionReference collectionReference = fStore.collection("Rewards");
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle("Delete");
builder.setMessage("Are you sure to delete " + (position + 1) + " reward?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Query query = collectionReference.whereEqualTo("donationReq", (position + 1));
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
for(DocumentSnapshot document : task.getResult()){
document.getReference().delete();
}
}
}
});
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.create().show();
}
});
}
Here is my Firebase if it helps:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…