【问题标题】:FirebaseUI - Populate RecyclerView on Dynamic Query DataFirebaseUI - 在动态查询数据上填充 RecyclerView
【发布时间】:2018-05-05 16:16:49
【问题描述】:

我在 firebase 中有一个 Collection,它有一定数量的项目,我们称之为 Collection“FOLLOWING”。这个项目的数量将不断变化。在任何给定时间,我都想监听这个Collection 中的项目数,并在基于另一个“集合”的Query 对象上创建几个whereEqualto() 调用,我们称之为“POLLS”:

        Query followingQuery = mStoreBaseRef.collection(USERS_LABEL).document(id).collection(FOLLOWING_LABEL);
        x = followingQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            //This number is constantly changing, and I want to use it to query the "Polls" node
            int numberOfUsersFollowed = documentSnapshots.size();
            if (documentSnapshots.isEmpty() || documentSnapshots == null) {
                mRecyclerview.setVisibility(View.INVISIBLE);
                return;
            } else {
                Log.v("NUBER_OF_USER_FOLLOWED", String.valueOf(numberOfUsersFollowed));
                mFollowingUserQuery = mStoreBaseRef.collection(POLLS_LABEL);
                //Based on the number of users being "Followed," I want to search for those users' respective polls in the "Polls" node, and populate the RecyclerView based on this information
                for (DocumentSnapshot x : documentSnapshots) {
                    Following followedUser = x.toObject(Following.class);
                    String userID = followedUser.getUser_id();
                    Log.v("FOLLOWED_USER_ID", userID);
                    mFollowingUserQuery = mFollowingUserQuery.whereEqualTo(USER_ID_LABEL, userID);
                }

                FirestoreRecyclerOptions<Poll> storeOptions = new FirestoreRecyclerOptions.Builder<Poll>()
                        .setQuery(mFollowingUserQuery, Poll.class)
                        .build();

                mFirestoreAdaper = new FirestoreRecyclerAdapter<Poll, PollHolder>(storeOptions) {
                    @Override
                    protected void onBindViewHolder(@NonNull final PollHolder holder, final int position, @NonNull Poll model) {
                        holder.mPollQuestion.setText(model.getQuestion());
                        String voteCount = String.valueOf(model.getVote_count());
                        //TODO: Investigate formatting of vote count for thousands
                        holder.mVoteCount.setText(voteCount);
                        Picasso.with(getActivity().getApplicationContext())
                                .load(model.getImage_URL())
                                .fit()
                                .into(holder.mPollImage);
                        holder.mView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent toClickedPoll = new Intent(getActivity(), PollHostActivity.class);
                                String recyclerPosition = getSnapshots().getSnapshot(position).getId();
                                Log.v("Firestore ID", recyclerPosition);
                                toClickedPoll.putExtra("POLL_ID", recyclerPosition);
                                startActivity(toClickedPoll);

                            }
                        });
                    }

                    @Override
                    public PollHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                        View v = LayoutInflater.from(parent.getContext())
                                .inflate(R.layout.latest_item, parent, false);
                        return new PollHolder(v);
                    }
                };

                mRecyclerview.setAdapter(mFirestoreAdaper);
                scrollToPosition();
                mFirestoreAdaper.startListening();

            }
        }
    });

基本上,whereEqualTo() 的数量是动态的。

编辑:我的 FirebaseUI RecylcerView 没有根据上面的查询填充任何数据。我的.onStart() 中有所有这些方法,所以我希望根据“关注”节点中的更改,它会动态填充,但它是空白的。

【问题讨论】:

    标签: java android firebase google-cloud-firestore firebaseui


    【解决方案1】:

    您似乎期望多个 where 子句充当逻辑 OR,并期望您将获得与您使用 whereEqualTo 指定的任何条件匹配的文档。这不是 Firestore 查询的工作方式。当您有多个条件时,它们充当逻辑 AND,这意味着 所有条件必须为真,文档才能匹配查询。

    如果您需要逻辑 OR,则必须对每个条件执行多个查询,然后将结果合并在一起。这意味着您将无法使用 FirestoreRecyclerAdapter,因为它需要一个查询。

    【讨论】:

    • 谢谢,Firebase 会考虑在未来实施吗?如果是这样,我可以提交功能请求。
    • 这已经被讨论了很多,但说出你的想法一个功能请求永远不会错。
    猜你喜欢
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 2019-03-09
    • 1970-01-01
    相关资源
    最近更新 更多