从 Firestore 集合中检索文档 ID (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) {


                }

            });

正如您所看到的,我正在使用下面显示的代码生成一个名为“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 中。


我该如何解决这两个问题?



繁花不似锦
浏览 139回答 3
3回答

收到一只叮咚

1)我用来提取文档 ID 的代码行不起作用,正在提取其他一些新的自动生成的 ID(我猜这是因为我在 .getId 之前使用了 .document() 方法() 方法)。不,这是因为您两次调用 CollectionReference 的document()方法:返回一个 DocumentReference 指向一个新文档,该文档具有此集合中自动生成的 ID。因此,每次调用document()方法时,都会生成一个新的 id。要解决此问题,请更改以下代码行:mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.document().set(myChatFields) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.addOnSuccessListener(/*&nbsp;...&nbsp;*/);到String&nbsp;id&nbsp;=&nbsp;mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.document().getId(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.document(id).set(myChatFields) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.addOnSuccessListener(/*&nbsp;...&nbsp;*/);看,我在引用中使用了id第一次生成的。现在在onSuccess()方法内部,您应该使用id上面生成的相同内容:myChatFields.put("chatCardId",&nbsp;id);而不是更新的,就像您在实际代码中所做的那样。2)由于某种原因,信息也没有通过我放置的最后一行代码添加到 HashMap 中。这是因为您使用了错误的参考。使用包含正确的参考id将解决您的问题。

开心每一天1111

对于第一部分,您需要在 onSuccess 函数与 void 中获取文档引用。所以这看起来像这样 -&nbsp; &nbsp; &nbsp; mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats").add(myChatFields)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onSuccess(DocumentReference documentReference) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String chatId = documentReference.getId();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.v("CHATS", chatId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myChatFields.put("chatCardId", chatId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent goToChatActivity = new Intent(UserProfileActivity.this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChatActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(goToChatActivity);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });

www说

您可以从集合数据中获取您的 id 作为 QuerySnapshot.Just 存储位置/id&nbsp; &nbsp; mFirebaseStore.collection("allorder").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onSuccess(QuerySnapshot queryDocumentSnapshots) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!queryDocumentSnapshots.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id = queryDocumentSnapshots.getDocuments().get(position).getId();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mArrayList.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<OrderDetails> data = queryDocumentSnapshots.toObjects(OrderDetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mArrayList.addAll(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mCustomerName.setText(mArrayList.get(position).getCustomerName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mCustomerContact.setText(mArrayList.get(position).getContactNumber());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mCustomerLocation.setText(mArrayList.get(position).getArea().equals("") ? "No Location Found" : mArrayList.get(position).getArea());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mCustomerInstruction.setText(mArrayList.get(position).getSpecialInstruction().equals("") ? "Type Something" : mArrayList.get(position).getSpecialInstruction());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mTotalPrice.setText(mArrayList.get(position).getTotalPrice().equals("") ? "0" : mArrayList.get(position).getTotalPrice().toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String orderStatus = mArrayList.get(position).getOrderStatus();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (orderStatus.equals("pending") || orderStatus.equals("rejected")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mLayout.setVisibility(View.VISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (orderStatus.equals("accepted")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mLayout.setVisibility(View.GONE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray json = new JSONArray(mArrayList.get(position).getItemList());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<AdminOrderDetailsModel> mList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < json.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = json.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AdminOrderDetailsModel model = new AdminOrderDetailsModel();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.setPackageName(jsonObject.getString("packageName"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.setPackageType(jsonObject.getString("packageType"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.setPrice(jsonObject.getString("price"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.setDuration(jsonObject.getString("duration"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.setQuantity(jsonObject.getInt("quantity"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mList.add(model);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AdminOrderDetailsAdapter adapter = new AdminOrderDetailsAdapter(getApplicationContext(), mList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setRecyclerLayoutManager(getApplicationContext(), mOrderListView, LinearLayoutManager.VERTICAL);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mOrderListView.setAdapter(adapter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ex.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java