英文:
how to delete a specific document from firebase firestore in RecyclerView
问题
在这段代码中,当我点击删除按钮时,它会删除Firestore中的所有文档,而不是删除特定的文档。
英文:
I have tried to delete a specific document form my firestore database
here is the rules of my 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;
}
}
}
And this is my java code is
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);
}
});
}
}
}
});
}
});
}
In this code when i click the delete button it deletes all the document in firestore instead of deleting a specific document.
答案1
得分: 1
你的代码问题在于你正在遍历集合中的所有文档,并逐个删除它们,而不是删除你想要删除的特定文档。你需要识别要删除的文档,然后在该特定文档上调用 delete() 方法。
holder.delet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取要删除的文档的ID
String id = datalist.get(position).getId();
db.collection("user data")
.document(id)
.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
// 从 datalist 中移除已删除的项目
datalist.remove(position);
// 通知适配器数据已更改
notifyDataSetChanged();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public onFailure(@NonNull Exception e) {
Log.w(TAG, "错误!", e);
}
});
}
});
英文:
The issue with your code is that you are iterating through all the documents in the collection and deleting them one by one, instead of deleting the specific document that you want to delete. You need to identify the document that you want to delete and then call the delete() method on that specific document.
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);
}
});
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论