Reputation: 155
I have got data structure like below. I need to a query for FirebaseRecyclerAdapter like this;
Query query = dbPosts.orderByChild("createDate").orderByChild("status").equlTo(0).orderByChild("voteSum").biggerThan(20);
I want to sort by the date of creation first, then sort by a specific situation, and finally, bring those who follow a certain situation.
I know there are not such as specifications but I think, you are understand what I mean it. So how can I do like this query or how can I do it in other ways to show my wishes to RecyclerView.
ps: Sorry for my poor English.
Data Structure
"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;
}
Upvotes: 0
Views: 1353
Reputation: 598740
First read my previous answer on the same topic: Query based on multiple where clauses in firebase.
You have a variation of that. You can combine the status
and voteSum
values in a single property and then perform the createdDate
sorting client-side.
"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
}
}
Now you can query for all items with status=0
and voteSum>20
with:
ref.orderByChild("status_voteSum").startAt("0_21")...
You'll still need to find a way to re-order the items on createdDate
client-side, since the approach can only be used for one relation operation (the value at the end).
In addition to the answer I linked, this approach is also covered in one of the episodes of our video series Firebase for SQL developers.
Upvotes: 2