1

Following this I am trying to fill my RecyclerView with posts from my Firestore.

My Firestore is structured exactly the same as in the linked answer so I am trying to replicate it. I think my issue is that I am not accessing the name of the documents properly. Would I have to use a .get() to retrieve the name? Below is what I have:

private CollectionReference userFollowingReference = rootRef.collection("following/"+ currentUser.getUid()+"/UserIsFollowing");
Query first = rootRef.collection("posts/"+ userFollowingReference.getId()+"/userPosts")
                .orderBy("date", Query.Direction.DESCENDING)
                .limit(3);
        first.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull final Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {
                        Post post = document.toObject(Post.class);
                        list.add(post);
                    }
                    followerAdapter.notifyDataSetChanged();

                    RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
                        @Override
                        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                            super.onScrollStateChanged(recyclerView, newState);
                            if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                                isScrolling = true;
                            }
                        }

                        @Override
                        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                            super.onScrolled(recyclerView, dx, dy);
                            lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
                            LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
                            int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                            int visibleItemCount = linearLayoutManager.getChildCount();
                            int totalItemCount = linearLayoutManager.getItemCount();

                            if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
                                isScrolling = false;
                                Query nextQuery = userFollowingReference.startAfter(lastVisible).limit(3);
                                nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<QuerySnapshot> t) {
                                        if (t.isSuccessful()) {
                                            for (DocumentSnapshot d : t.getResult()) {
                                                Post post = d.toObject(Post.class);
                                                list.add(post);
                                            }
                                            followerAdapter.notifyDataSetChanged();
                                            lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);

                                            if (t.getResult().size() < 3) {
                                                isLastItemReached = true;
                                            }
                                        }
                                    }
                                });
                            }
                        }
                    };
                    recyclerView.addOnScrollListener(onScrollListener);
                }
            }
        });

It should out put posts from all of the users that the current user is following but my output is blank

Edit: Here is a screenshot of my DB: enter image description here enter image description here The sub collection UserIsFollowing contains documents named after the users that the user is following whose only field is a boolean Set to true.

Edit 2:

I changed the code so that I first retrieve the list of followers then use that list to get their posts but this is still not working:

Query first = userFollowingReference;//TODO Realtime updates
        first.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull final Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {
                        String id = document.getId();
                        followingList.add(id);
                    }
                    followerAdapter.notifyDataSetChanged();
                    if(!followingList.isEmpty()) {
                        Query second = rootRef.collection("posts/" + followingList.get(0) + "/userPosts")
                                .orderBy("date", Query.Direction.ASCENDING)
                                .limit(3);
                        second.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull final Task<QuerySnapshot> task) {
                                if(task.isSuccessful()) {
                                    List<Post> list = new ArrayList<>();
                                    for(DocumentSnapshot document : task.getResult()){
                                        Post post = document.toObject(Post.class);
                                        list.add(post);
                                    }
                                    recyclerView.setAdapter(followerAdapter);
                                    followerAdapter.notifyDataSetChanged();
                                    lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);

                                    RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
                                        @Override
                                        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                                            super.onScrollStateChanged(recyclerView, newState);
                                            if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                                                isScrolling = true;
                                            }
                                        }

                                        @Override
                                        public void onScrolled(final RecyclerView recyclerView, int dx, int dy) {
                                            super.onScrolled(recyclerView, dx, dy);
                                            lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
                                            int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                                            int visibleItemCount = linearLayoutManager.getChildCount();
                                            int totalItemCount = linearLayoutManager.getItemCount();

                                            if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
                                                isScrolling = false;
                                                Query nextQuery = rootRef.collection("posts")
                                                        .orderBy("date", Query.Direction.ASCENDING)
                                                        .startAfter(lastVisible).limit(3);
                                                nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                                    @Override
                                                    public void onComplete(@NonNull Task<QuerySnapshot> t) {
                                                        if (t.isSuccessful()) {
                                                            for (DocumentSnapshot d : t.getResult()) {
                                                                Post post = d.toObject(Post.class);
                                                                postList.add(post);
                                                            }
                                                            followerAdapter.notifyDataSetChanged();
                                                            lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);

                                                            if (t.getResult().size() < 3) {
                                                                isLastItemReached = true;
                                                            }
                                                        }
                                                    }
                                                });
                                            }
                                        }
                                    };

                                    recyclerView.addOnScrollListener(onScrollListener);
                                }

                            }
                        });
                    }
                }
            }
        });

Edit 3:

I am getting this error:

E/RecyclerView: No adapter attached; skipping layout
    No adapter attached; skipping layout
David
  • 769
  • 1
  • 6
  • 28
  • Hi Bjorn! That's also an answer of mine so add your database structure as a screnshot please. – Alex Mamo Jul 04 '19 at 09:52
  • @Alex I added the requested screenshot. – David Jul 04 '19 at 15:12
  • Where is the `name` property, within `UserIsFollowing` or `userPosts`? Show us a more detailed screenshot. – Alex Mamo Jul 04 '19 at 15:15
  • Sorry Firebase only displays a collection, a document and a sub-collection at a time, Is the second image sufficient? ```UserIsFollowing``` contains documents who's Id's are that of the users the individual is following. – David Jul 04 '19 at 15:17
  • I cannot see any `name` property. – Alex Mamo Jul 04 '19 at 15:26
  • By name property do you mean the userID? It is simply the name of the document. – David Jul 04 '19 at 15:33
  • Yes, the name of the document. Are there at least the first three elements loaded? – Alex Mamo Jul 04 '19 at 15:42
  • No nothing is loading. I think the issue is with how I wrote my query. – David Jul 04 '19 at 15:43
  • Have you tried to use the exact doce in that answer and the same database structure? That example is a working example. – Alex Mamo Jul 04 '19 at 16:01
  • I thought that is what I was doing is there something that I am missing? – David Jul 04 '19 at 16:05
  • Check that code and compare it with yours. There is for sure something wrong in here, that I cannot see it right now. – Alex Mamo Jul 04 '19 at 16:06
  • @Alex I looked over my code and made some changes but it is still not displaying any posts. – David Jul 04 '19 at 17:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196012/discussion-between-bjorn-and-alex-mamo). – David Jul 04 '19 at 19:40

0 Answers0