RecyclerView上的删除按钮删除错误的项目 [英] Delete button on RecyclerView deletes the wrong item

查看:47
本文介绍了RecyclerView上的删除按钮删除错误的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的 RecyclerView 使用Firestore适配器,但无法使用删除"按钮.当我按下它时,它会删除错误的项目,而不是我想要的项目.

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.

这是 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();
        }
    });

}

如果有帮助,这是我的Firebase:

Here is my Firebase if it helps:

推荐答案

要从数据库中删除特定文档,您需要知道其ID.因此,这意味着当用户单击要删除的项目时,您将需要能够查找该项目的文档ID.因此,创建视图时,您需要同时从文档及其ID中获取值.

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.

因此,除了 RewardModel 对象之外,您还需要将相应的文档ID保留在某处.

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

如果您使用FirebaseUI显示列表,则可以通过以下操作获取给定位置的ID:

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?

这篇关于RecyclerView上的删除按钮删除错误的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆