Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

android - Delete button on RecyclerView deletes the wrong item

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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...