[英]Retrieving Document Id from Firestore Collection (Android)
我正在嘗試提取文檔下自動生成的 ID,以便我可以在其他地方使用它。
這是完整的代碼:
mStartChatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final HashMap<String, Object> myChatFields = new HashMap<>();
myChatFields.put("dateJoined", ServerValue.TIMESTAMP );
myChatFields.put("status", "");
myChatFields.put("snoozeUntil", "");
myChatFields.put("myCharacter", "");
myChatFields.put("requestedCustomChatCode", "");
myChatFields.put("groupChatName", "");
myChatFields.put("toInviteMembers", "");
myChatFields.put("lastMessage", "");
myChatFields.put("lastMessageTimeStamp", ServerValue.TIMESTAMP);
mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats")
.document().set(myChatFields)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
String chatId = mWhammyUsersCollection.document(mCurrentUserId)
.collection("my-chats").document().getId();
Log.v("CHATS", chatId);
myChatFields.put("chatCardId", chatId);
Intent goToChatActivity = new Intent(UserProfileActivity.this,
ChatActivity.class);
startActivity(goToChatActivity);
}
});
正如您所看到的,我正在使用下面顯示的代碼生成一個名為“my-chats”的集合,並且 .document() 正在創建自動生成的文檔 ID。
mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats")
.document().set(myChatFields)
.addOnSuccessListener(new OnSuccessListener<Void>() {
然后我使用下面顯示的代碼行嘗試從該文檔中獲取 id。
String chatId = mWhammyUsersCollection.document(mCurrentUserId)
.collection("my-chats").document().getId();
最后使用下面的代碼行,我試圖將它放入我創建的 HashMap 中。
myChatFields.put("chatCardId", chatId);
我有兩個主要問題:
1)我用來提取文檔 ID 的代碼行不起作用,正在提取其他一些新的自動生成的 ID(我猜這是因為我在 .getId 之前使用了 .document() 方法) () 方法)。
2)由於某種原因,信息也沒有通過我放置的最后一行代碼添加到 HashMap 中。
我該如何解決這兩個問題?
更形象地解釋一下:
我正在嘗試檢索“1”並將其添加到“2”的區域中。
1)我用來提取文檔 ID 的代碼行不起作用,正在提取其他一些新的自動生成的 ID(我猜這是因為我在 .getId 之前使用了 .document() 方法) () 方法)。
不,這是因為您兩次調用 CollectionReference 的document()方法:
返回一個 DocumentReference 指向一個新文檔,該文檔具有此集合中自動生成的 ID。
因此,每次調用document()
方法時,都會生成一個新的 id。 要解決此問題,請更改以下代碼行:
mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats")
.document().set(myChatFields)
.addOnSuccessListener(/* ... */);
到
String id = mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats")
.document().getId();
mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats")
.document(id).set(myChatFields)
.addOnSuccessListener(/* ... */);
看,我在引用中使用了第一次生成的id
。 現在在onSuccess()
方法中,您應該使用上面生成的相同id
:
myChatFields.put("chatCardId", id);
而不是更新的,就像您在實際代碼中所做的那樣。
2)由於某種原因,信息也沒有通過我放置的最后一行代碼添加到 HashMap 中。
這是因為您使用了錯誤的參考。 使用包含正確id
的引用將解決您的問題。
對於第一部分,您需要在 onSuccess 函數與 void 中獲取文檔引用。 所以這看起來像這樣 -
mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats").add(myChatFields)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
String chatId = documentReference.getId();
Log.v("CHATS", chatId);
myChatFields.put("chatCardId", chatId);
Intent goToChatActivity = new Intent(UserProfileActivity.this,
ChatActivity.class);
startActivity(goToChatActivity);
}
});
在文檔中寫道
但是,有時此文檔沒有有意義的 ID,讓 Cloud Firestore 自動為您生成 ID 會更容易。 您可以通過調用
add().
來完成此操作add().
所以你可以關注@R。 賴特回答。
您可以從集合數據中獲取您的 id 作為 QuerySnapshot.Just 存儲位置/id
mFirebaseStore.collection("allorder").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
try {
if (!queryDocumentSnapshots.isEmpty()) {
id = queryDocumentSnapshots.getDocuments().get(position).getId();
mArrayList.clear();
List<OrderDetails> data = queryDocumentSnapshots.toObjects(OrderDetails.class);
mArrayList.addAll(data);
mCustomerName.setText(mArrayList.get(position).getCustomerName());
mCustomerContact.setText(mArrayList.get(position).getContactNumber());
mCustomerLocation.setText(mArrayList.get(position).getArea().equals("") ? "No Location Found" : mArrayList.get(position).getArea());
mCustomerInstruction.setText(mArrayList.get(position).getSpecialInstruction().equals("") ? "Type Something" : mArrayList.get(position).getSpecialInstruction());
mTotalPrice.setText(mArrayList.get(position).getTotalPrice().equals("") ? "0" : mArrayList.get(position).getTotalPrice().toString());
String orderStatus = mArrayList.get(position).getOrderStatus();
if (orderStatus.equals("pending") || orderStatus.equals("rejected")) {
mLayout.setVisibility(View.VISIBLE);
} else if (orderStatus.equals("accepted")) {
mLayout.setVisibility(View.GONE);
}
JSONArray json = new JSONArray(mArrayList.get(position).getItemList());
List<AdminOrderDetailsModel> mList = new ArrayList<>();
for (int i = 0; i < json.length(); i++) {
JSONObject jsonObject = json.getJSONObject(i);
AdminOrderDetailsModel model = new AdminOrderDetailsModel();
model.setPackageName(jsonObject.getString("packageName"));
model.setPackageType(jsonObject.getString("packageType"));
model.setPrice(jsonObject.getString("price"));
model.setDuration(jsonObject.getString("duration"));
model.setQuantity(jsonObject.getInt("quantity"));
mList.add(model);
}
AdminOrderDetailsAdapter adapter = new AdminOrderDetailsAdapter(getApplicationContext(), mList);
setRecyclerLayoutManager(getApplicationContext(), mOrderListView, LinearLayoutManager.VERTICAL);
mOrderListView.setAdapter(adapter);
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.