Retrieving Document Id from Firestore Collection (Android)

后端 未结 3 676
攒了一身酷
攒了一身酷 2021-01-25 08:04

I\'m trying to extract the auto-generated Id under a document so I can use it elsewhere.

Here is the full code:

mStartChatButton.setOnClickListener(new V         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-25 08:15

    For part one, you'll want to get the document reference in the onSuccess function vs. void. So that would look something like this -

          mWhammyUsersCollection.document(mCurrentUserId).collection("my-chats").add(myChatFields)
                    .addOnSuccessListener(new OnSuccessListener() {
            @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);
    
                }
            });
    

提交回复
热议问题