【发布时间】: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