gpt4 book ai didi

java - FirebaseUI - 在动态查询数据上填充 RecyclerView

转载 作者:行者123 更新时间:2023-12-02 11:17:14 24 4
gpt4 key购买 nike

我在 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() 中有所有这些方法,因此我希望根据“Following”节点中的更改,它会动态填充,但它是空白的。

最佳答案

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

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

关于java - FirebaseUI - 在动态查询数据上填充 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50191521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com