我在firebase中有Collection
有一定数量的项目,我们称之为Collection
"关注"。这些项目将不断变化。在任何时候,我都想听取Collection
中的项目数量,并在基于另一个' Collection&#39的whereEqualto()
对象上创建多个Query
来电,让我们称之为" 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()
中拥有所有这些方法,所以我希望根据&#34;关注&#34;它将动态填充的节点,但它是空白的。
答案 0 :(得分:2)
看起来您期望多个where子句充当逻辑OR,期望您将获得与您使用{{1}指定的任何条件相匹配的文档}。这不是Firestore查询的工作方式。当您有多个条件时,它们将充当逻辑AND,这意味着所有条件必须为true才能使文档与查询匹配。
如果您需要逻辑OR,则必须对每个条件执行多个查询,然后将结果合并在一起。这意味着您将无法使用FirestoreRecyclerAdapter,因为它需要单个查询。