[英]How to delete Item from firestore in android with auto-generated document ID
我有一個分層形式的具有以下結構的 Firestore 數據庫:
集合(“notes”)> 文檔(currentUserId)> 集合(“mynotes”)> 文檔(自動生成的鍵)> 項目...
我已將數據添加到 Firestore,如下所示:
mAuth = FirebaseAuth.getInstance();
database = FirebaseFirestore.getInstance();
//here scheduleModel is a model class with constructor(String, boolean, String) with getters and
//setters for three of them
scheduleNoteModel = new ScheduleNote(noteTitle, isImp, strDate);
我已經添加了這樣的注釋項目。
database.collection("notes").document(mAuth.getUid())
.collection("mynotes")
.document() //generated id automatically...
.set(scheduleNoteModel)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(), "data added ...", Toast.LENGTH_SHORT).show();
etNoteTitle.setText("");
//dismiss (hide) the bottom sheet after adding item
BottomSheetScheduleFragment.this.dismiss();
//refresh the fragment
}
});
問題是在刪除項目時取回該 ID。 我知道有很多類似的問題,其中一些很好,但我無法解決它們。 我查看了此鏈接解決方案,但無法解決
在適配器中
holder.deleteNoteIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteNote(noteModel);
}
});
Method to delete
private void deleteNote(ScheduleNote note, int itemPosition, View view) {
mAuth = FirebaseAuth.getInstance();
database = FirebaseFirestore.getInstance();
//getting the doc id of auto-generated code in Firestore.
String id = database.collection("notes").document(mAuth.getUid()).collection("mynotes").document().getId();
Toast.makeText(context, "position " + itemPosition + "doc id " + id, Toast.LENGTH_SHORT).show();
database.collection("notes").document(mAuth.getUid())
.collection("mynotes")
//.document("72NMkKY73CXHVN7DFE8W") get that id automatically
.document(id)
.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
//notifyDataSetChanged();
Snackbar snackbar = Snackbar.make(view, "Note Deleted successfully", Snackbar.LENGTH_SHORT);
snackbar.show();
}
});
}
上述變量中的 id 與真實 id 不匹配。 怎么得到那個id?
我基本上想要兩個信息:
我實際上做了很多研究,但無法弄清楚。 任何快速幫助將不勝感激,謝謝。
您似乎沒有遵循此鏈接解決方案。 首先,正如@Frank van Puffelen 在他的評論中提到的那樣,“document().getId()”每次調用都會生成一個全新的 ID。 因此,您應該將該 ID 保存到一個變量中以供以后使用,這樣您就可以直接從適配器中刪除所需的文檔。 您的“ScheduleNote”class,除了“noteTitle”、“isImp”和“strDate”字段外,還應該包含一個名為“id”的字段。 所以你的聲明 class 應該是這樣的:
class ScheduleNote {
public String noteTitle, strDate, id;
public boolean isImp;
public ScheduleNote() {}
public ScheduleNote(String noteTitle, String strDate, String id, boolean isImp) {
this.noteTitle = noteTitle;
this.strDate = strDate;
this.id = id;
this.isImp = isImp;
}
}
現在,在您的特定情況下,將 object 添加到 Firestore 的以下代碼行將起到作用:
CollectionRefference mynotes = database.collection("notes").document(mAuth.getUid())
.collection("mynotes")
String docId = mynotes.document().getId();
要創建“ScheduleNote”類型的 object,請使用:
ScheduleNote scheduleNoteModel = new ScheduleNote(noteTitle, strDate, docId, isImp);
// ^
// newly added
要將數據實際寫入 Firestore,請使用:
mynotes.document(docId).set(scheduleNoteModel).addOnSuccessListener(/* ... */);
// ^
為了能夠刪除該文檔,您應該使用以下代碼行:
database.collection("notes").document(mAuth.getUid())
.collection("mynotes")
.document(id)
.delete(note.id)
.addOnSuccessListener(/* ... */);
這是因為,以下行再次生成了一個新的 ID,這是不正確的,因為您需要使用之前生成的 ID:
String id = database.collection("notes").document(mAuth.getUid()).collection("mynotes").document().getId();
首先將文檔添加到您的集合中,其中沒有任何數據。
DocumentReference addedDocRef =database.collection("notes").document(mAuth.getUid())
.collection("mynotes")
.document();
log.i("Added document with ID: " + addedDocRef.getId());
然后將文檔 ID 添加到您的 model class 以供以后使用。
現在,使用您的 model class 為文檔設置數據。
addedDocRef.set(scheduleNoteModel)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.