我有如下数据结构。我需要像这样查询FirebaseRecyclerAdapter;
Query query = dbPosts.orderByChild("createDate").orderByChild("status").equlTo(0).orderByChild("voteSum").biggerThan(20);
我想先按照创建日期排序,然后根据具体情况排序,最后,让那些跟踪某个情况的人接受。
我知道没有规格,但我想,你明白我的意思。那么我该怎么做这样的查询呢?我怎么能用其他方式来表达我对RecyclerView的愿望。
ps:抱歉我的英语不好。
数据结构
"posts" : {
"-K_FaI8rsasB0hvFNTTv" : {
"category" : 0, -> int
"createdDate" : 1483115934775,
"image" : "https://firebasestorage.googleapis.com/...",
"status" : 0, -> int
"title" : "sadfsadfsadf",
"userId" : "iuHJQ044GrMPjRMF1CxPq15tp6g2",
"username" : "someone",
"voteSum" : 34 -> Long
},
"-K_GI57zSiPP6ETgctPD" : {
"category" : 0, -> int
"createdDate" : 1483127677924,
"image" : "https://firebasestorage.googleapis.com/...",
"status" : 0, ->int
"title" : "qwewqeqwe",
"userId" : "MWv568D0f3VO1y683kZumOQ7gHZ2",
"username" : "idiots",
"voteSum" : 13 -> Long
}
}
FirebaseRecyclerAdapter:
public FirebaseRecyclerAdapter recyclerAdapter(Query query) {
FirebaseRecyclerAdapter<Post, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(
Post.class,
R.layout.card_view_post,
PostViewHolder.class,
query
) {
@Override
protected void populateViewHolder(final PostViewHolder viewHolder, Post model, int position) {
final String postId = getRef(position).getKey();
// TODO sum vote
viewHolder.setTitle(model.getTitle());
viewHolder.setImage(getContext(), model.getImage());
viewHolder.setSumVotes(postId);
viewHolder.setSumComments(postId);
if (checkAuthUser()) {
viewHolder.setUpVote(postId);
viewHolder.setDownVote(postId);
}
viewHolder.txtTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), SinglePostActivity.class);
intent.putExtra(Enums.PostKeys.postId.getValue(), postId);
startActivity(intent);
}
});
viewHolder.imvImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), SinglePostActivity.class);
intent.putExtra(Enums.PostKeys.postId.getValue(), postId);
startActivity(intent);
}
});
viewHolder.imbComment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), CommentsActivity.class);
intent.putExtra(Enums.PostKeys.postId.getValue(), postId);
startActivity(intent);
}
});
viewHolder.imbUpVote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!checkAuthUser()) {
startActivity(new Intent(getActivity(), SignUpActivity.class));
return;
}
processVote = true;
Singleton.getDbPostDownVote().child(postId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (processVote == true) {
if (dataSnapshot.hasChild(getUserId())) {
Singleton.getDbPostDownVote().child(postId).child(getUserId()).removeValue();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Singleton.getDbPostUpVote().child(postId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (processVote == true) {
if (dataSnapshot.hasChild(getUserId())) {
Singleton.getDbPostUpVote().child(postId).child(getUserId()).removeValue();
processVote = false;
} else {
Singleton.getDbPostUpVote().child(postId).child(getUserId()).setValue(0);
processVote = false;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
viewHolder.imbDownVote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!checkAuthUser()) {
startActivity(new Intent(getActivity(), SignUpActivity.class));
return;
}
processVote = true;
Singleton.getDbPostUpVote().child(postId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (processVote == true) {
if (dataSnapshot.hasChild(getUserId())) {
Singleton.getDbPostUpVote().child(postId).child(getUserId()).removeValue();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Singleton.getDbPostDownVote().child(postId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (processVote == true) {
if (dataSnapshot.hasChild(getUserId())) {
Singleton.getDbPostDownVote().child(postId).child(getUserId()).removeValue();
processVote = false;
} else {
Singleton.getDbPostDownVote().child(postId).child(getUserId()).setValue(1);
processVote = false;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
viewHolder.imbMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = new ContextThemeWrapper(getContext(), R.style.popupMenuStyle);
PopupMenu popup = new PopupMenu(context, viewHolder.imbMenu);
//inflating menu from xml resource
popup.inflate(R.menu.post_menu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.deletePost:
break;
case R.id.reportPost:
break;
}
return false;
}
});
//displaying the popup
popup.show();
}
});
}
};
return firebaseRecyclerAdapter;
}
答案 0 :(得分:2)
首先阅读我之前关于同一主题的回答:Query based on multiple where clauses in firebase。
你有一个变化。您可以将status
和voteSum
值合并到一个属性中,然后执行createdDate
排序客户端。
"posts" : {
"-K_FaI8rsasB0hvFNTTv" : {
"status_voteSum": "0_34",
"category" : 0,
"createdDate" : 1483115934775,
"image" : "https://firebasestorage.googleapis.com/...",
"status" : 0,
"title" : "sadfsadfsadf",
"userId" : "iuHJQ044GrMPjRMF1CxPq15tp6g2",
"username" : "someone",
"voteSum" : 34
},
"-K_GI57zSiPP6ETgctPD" : {
"status_voteSum": "0_13",
"category" : 0,
"createdDate" : 1483127677924,
"image" : "https://firebasestorage.googleapis.com/...",
"status" : 0,
"title" : "qwewqeqwe",
"userId" : "MWv568D0f3VO1y683kZumOQ7gHZ2",
"username" : "idiots",
"voteSum" : 13
}
}
现在,您可以使用{<1}}和status=0
查询所有项目:
voteSum>20
您仍然需要找到一种方法来重新排序ref.orderByChild("status_voteSum").startAt("0_21")...
客户端上的项目,因为该方法只能用于一个关系操作(最后的值)。 / p>
除了我链接的答案之外,我们的视频系列Firebase for SQL developers的其中一部分也涵盖了这种方法。