Monday, 14 June 2021

Java Firestore Android use an arraylist in a query to show posts from followed users

I am developing an activity that displays the posts of the users that the user follows. For the posts I used the following structure:

  root --- posts (collection)
             |
             --- uid (documents)
                  |
                  --- userPosts (collection)
                        |
                        --- postId (documents)
                        |     |
                        |     --- title: "Post Title"
                        |     |
                        |     --- date: 4/06/2021
                        |
                        --- postId (documents)
                              |
                              --- title: "Post Title"
                              |
                              --- date: 08/06/2021
                              |
                              --- description: "this is the description of the post"
                              [...etc]

Lately I did an activity that visualized the posts of a user and that's it. Now, instead, I want to show the posts of the users that the user follows in the home page. This is the structure of the "following": posts (collection)

   root--- folowing(documents)
              |
              --- uid(collection)
                    |
                    --- userFollowing(documents)
                          |
                          --- followed user(collection) -- followed user(document)
                          |
                          --- followed user (collection) -- followed user(document)

So, I tried to collect on an Arraylist all user uids that are followed by the user in this way:

FirebaseFirestore.getInstance().collection("following").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("userFollowing").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    document.getData();
                    Uidrecord.add(cont,String.valueOf(document.getData()));
                    cont++;
                }
                int conta = 0;
                for (String item: Uidrecord){
                    if(item == null)
                        break;
                    uidFollowing.add(conta,getFollowingUid(item));
                    Toast.makeText(MainActivity.this, uidFollowing.get(conta), Toast.LENGTH_SHORT).show();
                    conta++;

                }
            } else {
                Toast.makeText(MainActivity.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
            }
        }
    });

(uid Following contains all the ids of a user) This is how I set up the query to fetch a single user's post data:

Query query = FirebaseFirestore.getInstance()
                .collection("post/" + uidVisit + "/userPosts")
                .limit(1000);

I knew the user id I had to look for posts in, so it was a static query because I had to get a user's posts and that's it. Now instead the uid where to get the posts are contained in an arraylist. So I was wondering how I could create a dynamic query and then how to grab the posts of every single user followed (using RecyclerView). It's possible to do it?

Edit: As you can see from my complete code https://codeshare.io/MNWYb3 I tried putting a for (line 225) and dynamizing the query, but the result is that it only shows the posts of a user and that's it (it seems to overwrite a each for the adapter, although I'm not sure about that)



from Java Firestore Android use an arraylist in a query to show posts from followed users

No comments:

Post a Comment