Delete button on RecyclerView deletes the wrong item

a 夏天 提交于 2021-01-20 13:36:20

问题


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:


回答1:


To delete a specific document from the database you'll need to know its ID. So that means when the user clicks on an item to delete, you'll need to be able to look up that item's document ID. For that reason you'll need to get both the values from the document and its ID when you're creating the view.

So in addition to the RewardModel objects, you'll also need to keep the corresponding document IDs somewhere.

If you're using FirebaseUI to show the list, you can get the ID for a given position by following: Is there a way to get document ID of item from FirestoreRecyclerAdapter?




回答2:


You are sending wrong data.

Query query = collectionReference.whereEqualTo("donationReq", (position + 1));

You had write position instead of position + 1



来源:https://stackoverflow.com/questions/65579788/delete-button-on-recyclerview-deletes-the-wrong-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!