【问题标题】:Android Firestore: order posts by document valueAndroid Firestore:按文档值排序帖子
【发布时间】:2020-04-20 14:33:33
【问题描述】:

我有以下数据库配置: enter image description here

collection Posts -> postID -> 这里是另一个名为 LikesNumber 的集合 -> 文档 currentLikesNumber -> 字段 currentNoOfLikes

在我的应用程序中,我想根据某些功能对我的帖子进行排序。我已经实现了按帖子类别排序的代码,它是这样的:

public void getHorror(){
    blog_list = new ArrayList<>();
    blogRecyclerAdapter = new BlogRecyclerAdapter(blog_list);
    blog_list_view.setLayoutManager(new LinearLayoutManager(getActivity()));
    blog_list_view.setAdapter(blogRecyclerAdapter);
    blog_list_view.setHasFixedSize(true);
    firebaseAuth = FirebaseAuth.getInstance();


    if(firebaseAuth.getCurrentUser() != null) {
        firebaseFirestore = FirebaseFirestore.getInstance();
        blog_list_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Boolean reachedBottom = !recyclerView.canScrollVertically(1); //if we reach bottom
                if(reachedBottom){

                    loadMore();
                }
            }
        });

        Query firstQuery = firebaseFirestore.collection("Posts").whereEqualTo("category","Horror").orderBy("timestamp", Query.Direction.DESCENDING); //show newest posts first
        firstQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {

            @Override
            public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e) {
                if (e == null) {
                    if (!queryDocumentSnapshots.isEmpty()) {
                        if (isFirstPageLoaded) {
                            lastVisible = queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.size() - 1);
                            blog_list.clear();
                        }
                        for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
                            if (doc.getType() == DocumentChange.Type.ADDED) {
                                String blogPostId = doc.getDocument().getId();
                                BlogPost blogPost = doc.getDocument().toObject(BlogPost.class).withId(blogPostId);
                                if (isFirstPageLoaded) {
                                    blog_list.add(blogPost);
                                } else {
                                    blog_list.add(0, blogPost);
                                }
                                blogRecyclerAdapter.notifyDataSetChanged();
                            }
                        }
                        isFirstPageLoaded = false;
                    }
                }
            }
        });
    }
    blog_list.clear();
    blogRecyclerAdapter.notifyDataSetChanged();
}

基本上,当类别字段中包含给定的字符串时,我的回收器适配器将更新,仅显示那些特定的帖子。

现在,我想做同样的事情,但我想按currentNoOfLikes 从高到低对它们进行排序。我的问题是我不知道如何访问 currentNoOfLikes 中的数据。这是我尝试过的

public void getLikesHL(){
    blog_list = new ArrayList<>();
    blogRecyclerAdapter = new BlogRecyclerAdapter(blog_list);
    blog_list_view.setLayoutManager(new LinearLayoutManager(getActivity()));
    blog_list_view.setAdapter(blogRecyclerAdapter);
    blog_list_view.setHasFixedSize(true);
    firebaseAuth = FirebaseAuth.getInstance();


    if(firebaseAuth.getCurrentUser() != null) {
        firebaseFirestore = FirebaseFirestore.getInstance();
        blog_list_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Boolean reachedBottom = !recyclerView.canScrollVertically(1); //if we reach bottom
                if(reachedBottom){

                    loadMore();
                }
            }
        });

        Query firstQuery = firebaseFirestore.collection("LikesNumber").orderBy("currentNoOfLikes",Query.Direction.DESCENDING); //show newest posts first
        firstQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {

            @Override
            public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e) {
                if (e == null) {
                    if (!queryDocumentSnapshots.isEmpty()) {
                        if (isFirstPageLoaded) {
                            lastVisible = queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.size() - 1);
                            blog_list.clear();
                        }
                        for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
                            if (doc.getType() == DocumentChange.Type.ADDED) {
                                String blogPostId = doc.getDocument().getId();
                                BlogPost blogPost = doc.getDocument().toObject(BlogPost.class).withId(blogPostId);
                                if (isFirstPageLoaded) {
                                    blog_list.add(blogPost);
                                } else {
                                    blog_list.add(0, blogPost);
                                }
                                blogRecyclerAdapter.notifyDataSetChanged();
                            }
                        }
                        isFirstPageLoaded = false;
                    }
                }
            }
        });
    }
    blog_list.clear();
    blogRecyclerAdapter.notifyDataSetChanged();
}

但是当recycler适配器更新时,它是空的。由于某种原因,我无法从 currentNoOfLikes 获取数据并将其从最高数值更新为最低数值(降序)。

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

    改变这个:

    Query firstQuery = firebaseFirestore.collection("LikesNumber").orderBy("currentNoOfLikes",Query.Direction.DESCENDING); //show newest posts first
    

    进入这个:

    firebaseFirestore = FirebaseFirestore.getInstance();
    Query firstQuery = firebaseFirestore.collection("Posts").document(postID).collection("LikesNumber").orderBy("currentNoOfLikes",Query.Direction.DESCENDING);
    

    集合LikesNumber 是一个子集合,因此要访问它,您需要通过 Posts -> postsID -> LikesNumber 才能获取数据。

    【讨论】:

      猜你喜欢
      • 2014-06-23
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      相关资源
      最近更新 更多