title: Firestore how to retrieve data from firestore in recycler view
body: <p><strong>Background</strong></p>
  
  <p>My app allows users to post images in specific categories and then allows users to tap on the posts to bring up a messaging activity</p>
  
  <p><strong>Problem</strong>  </p>
  
  <p>I currently have it setup so it is a global chat (any user could join and it was the same between all posts reading and writing from the same document) for testing purposes but I want to have it so it is a private chat between the two users. This was created using the real-time database I am in the process of migrating over to Firestore so I will also have to change the code for the "chatActivity"</p>
  
  <p><strong>What I have done</strong></p>
  
  <p>When the post is created it adds a new document to the messages collection for that post. The messages document name associated with that post is then stored in the post. </p>
  
  <p><strong>Where I am stuck</strong></p>
  
  <p>In my chat activity, I need to be able to get the id of the post so I can then retrieve the location of the document containing the messages related to that post</p>
  
  <p><strong>Objective</strong></p>
  
  <p>To be able to have the users post an image and have a document in the "Messages" collection be created DONE, then to have a second user come and see said image tap on it and then be able to open that document that was created for the image so the two users can then exchange messages between each other making it private between the two users because they are only reading from the document associated with that post</p>
  
  <p><a href="https://pasteboard.co/I6x0xEJ.png" rel="nofollow noreferrer"><strong>app workflow</strong></a> this should clear up any confusion</p>
  
  <p>Database: <a href="https://i.stack.imgur.com/rMm4o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rMm4o.png" alt="database"></a></p>
  
  <p>Code for writing post to database:</p>
  
  <pre><code>filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener&lt;Uri&gt;() {
                                  @Override
                                  public void onSuccess(Uri uri) {
                                      final String downloadUrl =
                                              uri.toString();
                                      Log.d("tag", downloadUrl);
  
                                      FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
                                      String uid = Objects.requireNonNull(current_user).getUid();
  
                                      final Map&lt;String, Object&gt; postMap = new HashMap&lt;&gt;();
                                      // No thumb ?????
                                      postMap.put("image_url", downloadUrl);
                                      postMap.put("desc", desc);
                                      postMap.put("user_id", current_user_id);
                                      postMap.put("message Doc", uid + postCategory);
                                      postMap.put("timestamp", FieldValue.serverTimestamp());
  
  
                                      firebaseFirestore.collection(postCategory).add(postMap).addOnCompleteListener(new OnCompleteListener&lt;DocumentReference&gt;() {
                                          @Override
                                          public void onComplete(@NonNull Task&lt;DocumentReference&gt; task) {
  
                                              if (task.isSuccessful()) {
  
                                                  firebaseFirestore.collection("Posts").add(postMap).addOnCompleteListener(new OnCompleteListener&lt;DocumentReference&gt;() {
                                                      @Override
                                                      public void onComplete(@NonNull Task&lt;DocumentReference&gt; task) {
  
                                                          FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
                                                          String uid = Objects.requireNonNull(current_user).getUid();
  
                                                          final Map&lt;String, String&gt; chatMap = new HashMap&lt;&gt;();
                                                          postMap.put("timestamp", FieldValue.serverTimestamp());
                                                          postMap.put("name", current_user_id);
                                                          postMap.put("message", "");
  
                                                          firebaseFirestore.collection("Messages")
                                                                  .document(uid + postCategory)
                                                                  .set(chatMap)
                                                                  .addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
                                                                      @Override
                                                                      public void onSuccess(Void aVoid) {
  
                                                                      }
                                                                  });
  </code></pre>
  
  <p>Code for chat </p>
  
  <pre><code>public class ChatActivity extends AppCompatActivity {
  
      public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000;
      private static final int GALLERY_PICK = 1;
  
      private ListView mMessageListView;
      private MessageAdapter mMessageAdapter;// This is to do with the file messageadapter\
      private ProgressBar mProgressBar;
      private ImageButton mPhotoPickerButton;
      private EditText mMessageEditText;
      private Button mSendButton;
      private String mUsername;
      private FirebaseDatabase mFirebaseDatabase;
      private DatabaseReference mMessagedatabaseReference;
      private ChildEventListener mChildEventListner;
      private ValueEventListener mValueEventListner;
      private FirebaseUser mCurrentUser;
      private FirebaseStorage mFirebaseStorage;
      private ProgressDialog mProgressDialog;
      private StorageReference mChatPhotosStorageReference;
  
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.chat_activity);
  
          mFirebaseDatabase = FirebaseDatabase.getInstance();
          mMessagedatabaseReference = mFirebaseDatabase.getReference().child("messages");
  
  
          //new shit
  
          // Map&lt;String, Object&gt; usersChat = new HashMap&lt;&gt;();
          // usersChat.put("user 1 id", mCurrentUser);
          // usersChat.put("user2Id", )
  
  
  
  
  
          mFirebaseStorage = FirebaseStorage.getInstance();
          mChatPhotosStorageReference = mFirebaseStorage.getReference().child("chat_photos");
  
          // Initialize references to views
          mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
          mMessageListView = (ListView) findViewById(R.id.messageListView);
          mPhotoPickerButton = (ImageButton) findViewById(R.id.photoPickerButton);
          mMessageEditText = (EditText) findViewById(R.id.messageEditText);
          mSendButton = (Button) findViewById(R.id.sendButton);
  
          mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
          final String current_uid = mCurrentUser.getUid();
  
          // Initialize progress bar
          mProgressBar.setVisibility(ProgressBar.INVISIBLE);
  
          //Initialize message ListView and its adapter
          List&lt;FriendlyMessage&gt; friendlyMessages = new ArrayList&lt;&gt;();
          mMessageAdapter = new MessageAdapter(this, R.layout.item_message, friendlyMessages);
          mMessageListView.setAdapter(mMessageAdapter);
  
          // ImagePickerButton shows an image picker to upload a image for a message
          mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent galleryIntent = new Intent();
                  galleryIntent.setType("image/*");
                  galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
  
                  startActivityForResult(Intent.createChooser(galleryIntent, "Select Image"), GALLERY_PICK);
              }
          });
  
          // Enable Send button when there's text to send
          mMessageEditText.addTextChangedListener(new TextWatcher() {
              @Override
              public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
              }
  
              @Override
              public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                  if (charSequence.toString().trim().length() &gt; 0) {
                      mSendButton.setEnabled(true);
                  } else {
                      mSendButton.setEnabled(false);
                  }
              }
  
              @Override
              public void afterTextChanged(Editable editable) {
              }
          });
          mMessageEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT)});
  
          // Send button sends a message and clears the EditText
          mSendButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString());
                  mMessagedatabaseReference.push().setValue(friendlyMessage);
  
                  // Clear input box
                  mMessageEditText.setText("");
              }
          });
  
          mChildEventListner = new ChildEventListener() {
              @Override
              public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                  FriendlyMessage friendlyMessage = dataSnapshot.getValue(FriendlyMessage.class);
                  mMessageAdapter.add(friendlyMessage);
  
              }
  
              @Override
              public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
  
              }
  
              @Override
              public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
  
              }
  
              @Override
              public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
  
              }
  
              @Override
              public void onCancelled(@NonNull DatabaseError databaseError) {
  
              }
          };
          mMessagedatabaseReference.addChildEventListener(mChildEventListner);
  
          mSendButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), current_uid, null);
                  mMessagedatabaseReference.push().setValue(friendlyMessage);
  
                  // Clear input box
                  mMessageEditText.setText("");
              }
          });
  
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          if (requestCode == GALLERY_PICK &amp;&amp; resultCode == RESULT_OK) {
  
              Uri selectedImageUri = data.getData();
  
              final StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment());
  
              photoRef.putFile(selectedImageUri).addOnSuccessListener(new OnSuccessListener&lt;UploadTask.TaskSnapshot&gt;() {
                  @Override
                  public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                      photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener&lt;Uri&gt;() {
                          @Override
                          public void onSuccess(Uri uri) {
                              String downloadUrl = uri.toString();
                              Log.d("tag", downloadUrl);
                              FriendlyMessage friendlyMessage = new FriendlyMessage(null, mUsername, downloadUrl);
                              mMessagedatabaseReference.push().setValue(friendlyMessage);
                          }
                      });
                  }
              });
  
          }
      }
  </code></pre>