如何在RecyclerView中从Firebase Firestore中删除特定文档

rdlzhqv9  于 2023-04-12  发布在  其他
关注(0)|答案(1)|浏览(125)

我试图从我的firestore数据库中删除一个特定的文档
这是我消防店的规矩

// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

这是我的java代码

public void onBindViewHolder(@NonNull myviewholder holder, @SuppressLint("RecyclerView") int position) {
    holder.tname.setText(datalist.get(position).getName());
    holder.ttype.setText(datalist.get(position).getType());
    holder.tdescription.setText(datalist.get(position).getDescription());

    holder.delet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            collectionRef.get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if(task.isSuccessful()){
                            for (QueryDocumentSnapshot document :task.getResult()){
                                String id = document.getId();
                                db.collection("user data")
                                    .document(id)
                                    .delete()
                                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                                        @Override
                                        public void onSuccess(Void unused) {
                                           notifyDataSetChanged();
                                        }
                                    }).addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Log.w(TAG, "Error!", e);
                                        }
                                    });
                            }
                        }
                    }
                });
        }
    });
}

在这段代码中,当我单击delete按钮时,它会删除firestore中的所有文档,而不是删除特定的文档。

92vpleto

92vpleto1#

代码的问题是,您正在迭代集合中的所有文档并逐个删除它们,而不是删除要删除的特定文档。您需要标识要删除的文档,然后对该特定文档调用delete()方法。

holder.delet.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Get the ID of the document you want to delete
        String id = datalist.get(position).getId();

        db.collection("user data")
            .document(id)
            .delete()
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void unused) {
                    // Remove the deleted item from the datalist
                    datalist.remove(position);
                    // Notify the adapter that the data has changed
                    notifyDataSetChanged();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.w(TAG, "Error!", e);
                }
            });
    }
});

相关问题