首页 > 解决方案 > Java Android 使用 ListAdapter 显示关注用户的帖子

问题描述

我正在开发一个显示用户关注的用户帖子的活动。对于帖子,我使用了以下结构:

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

我正在寻找一种在查询中使用数组列表来显示关注用户的帖子Java Firestore Android 在查询中使用数组列表来显示关注用户的帖子但得出的结论是您不能使用 FirestoreRecyclerAdapter 来使用相同的查询,从而显示关注用户的帖子,因为 recyclerview 每次都会覆盖自己,并且只会输出 arraylist 中最后一个用户 ID 的帖子。

有人告诉我使用 ListAdapter,所以我尝试做一些研究,但找不到任何对我有帮助的东西。

使用 RecyclerView,我使用以下 get 和 set 类从 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; }
}

虽然这是我之前一直使用 RecyclerView 的代码:https ://codeshare.io/r9vRXA

这是我到目前为止所做的,但我不知道如何设置程序以及如何找到我的解决方案:

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();

                }

有人知道如何帮助我吗?

- 完整代码https://codeshare.io/Pdk1ee

标签: javaandroidgoogle-cloud-firestorelistadapter

解决方案


推荐阅读