Friday, 18 June 2021

Java Android Use ListAdapter to show posts of 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:

    |
     --- 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]

I was looking for a way to use an arraylist in a query to show the posts of the followed users Java Firestore Android use an arraylist in a query to show posts from followed users but it came to the conclusion that you cannot use the FirestoreRecyclerAdapter to use the same query and thus show the posts of the followed users because the recyclerview would overwrite itself every time and would output only the post of the last user id in the arraylist.

I was told to use ListAdapter so I tried to do some research but couldn't find anything that helped me in my case.

With RecyclerView I used the following get and set class to get the data from the firestore:

package com.conta.pophome;

public class GetInfoPost {

    String title, urlImage, genre, valutation, date, description, like;

    public GetInfoPost() {
    }

    public GetInfoPost( String title, String urlImage, String genre, String valutation, String date, String description, String like) {
        this.title = title;
        this.urlImage = urlImage;
        this.genre = genre;
        this.valutation = valutation;
        this.date = date;
        this.description = description;
    }

    public String getLike() {
        return like;
    }

    public void setLike(String like) {
        this.like = like;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrlImage() {
        return urlImage;
    }

    public void setUrlImage(String urlImage) {
        this.urlImage = urlImage;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getValutation() {
        return valutation;
    }

    public void setValutation(String valutation) {
        this.valutation = valutation;
    }

    public String getdate() {
        return date;
    }

    public void setdate(String date) { this.date = date; }
}

While this was the code I used before always with RecyclerView: https://codeshare.io/r9vRXA

This is what I have done so far, but I don't know how to set up the procedure and how to get to my solution:

for (int i = 0; i < uidFollowing.size(); i++) {

                    Query query = FirebaseFirestore.getInstance()
                            .collection("post/" + uidFollowing.get(i) + "/userPosts")
                            .limit(1000);
                    Toast.makeText(MainActivity.this, uidFollowing.get(i) + "", Toast.LENGTH_SHORT).show();


        /*query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        Toast.makeText(visualizzaPost.this, document.getId() + " => " + document.getData(), Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    Toast.makeText(visualizzaPost.this, "error", Toast.LENGTH_SHORT).show();
                }
            }
        });*/


                    FirestoreRecyclerOptions<GetInfoPost> options = new FirestoreRecyclerOptions.Builder<GetInfoPost>()
                            .setQuery(query, GetInfoPost.class)
                            .build();

                    



                    int finalI = i;
                    firestoreRecyclerAdapter = new ListAdapter() {
                        @Override
                        public boolean areAllItemsEnabled() {
                            return false;
                        }

                        @Override
                        public boolean isEnabled(int position) {
                            return false;
                        }

                        @Override
                        public void registerDataSetObserver(DataSetObserver observer) {

                        }

                        @Override
                        public void unregisterDataSetObserver(DataSetObserver observer) {

                        }

                        @Override
                        public int getCount() {
                            return 0;
                        }

                        @Override
                        public Object getItem(int position) {
                            return null;
                        }

                        @Override
                        public long getItemId(int position) {
                            return 0;
                        }

                        @Override
                        public boolean hasStableIds() {
                            return false;
                        }

                        @Override
                        public View getView(int position, View convertView, ViewGroup parent) {
                            return null;
                        }

                        @Override
                        public int getItemViewType(int position) {
                            return 0;
                        }

                        @Override
                        public int getViewTypeCount() {
                            return 0;
                        }

                        @Override
                        public boolean isEmpty() {
                            return false;
                        }
                    };



                    //Toast.makeText(MainActivity.this, uidFollowing.isEmpty() + "", Toast.LENGTH_SHORT).show();

                }

Would anyone know how to help me?

-Full code https://codeshare.io/Pdk1ee



from Java Android Use ListAdapter to show posts of followed users

No comments:

Post a Comment