Comprehensive Firebase Query for Android

I have a data structure as shown below. I need a query for FirebaseRecyclerAdapter as follows:

Query query = dbPosts.orderByChild("createDate").orderByChild("status").equlTo(0).orderByChild("voteSum").biggerThan(20); 

I want to sort the creation date first, then sort by the specific situation and finally bring those who follow the specific situation.

I know there are no such specifications, but I think you understand what I mean. So, how can I make this request or how to do it in other ways to show my wishes of RecyclerView.

ps: Sorry for the bad 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; } 
0
source share
1 answer

First read my previous answer on the same topic: Query based on multiple sentences in firebase .

You have a variant of this. You can combine the status and voteSum in one property, and then sort on the createdDate 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 elements with status=0 and voteSum>20 with:

 ref.orderByChild("status_voteSum").startAt("0_21")... 

You still need to find a way to reorder the createdDate client-side createdDate , as this approach can only be used for one relationship operation (value at the end).

In addition to the answer I linked, this approach is also covered in one of the episodes of our Firebase SQL developer video.

+2
source

Source: https://habr.com/ru/post/1265418/


All Articles