title: Complex query on Firebase for Android
body: <p>I have got data structure like below. I need to a query for FirebaseRecyclerAdapter like this;</p>
  
  <pre><code> Query query = dbPosts.orderByChild("createDate").orderByChild("status").equlTo(0).orderByChild("voteSum").biggerThan(20);
  </code></pre>
  
  <p>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.</p>
  
  <p>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 <strong>other ways</strong> to show my wishes to RecyclerView.</p>
  
  <p>ps: Sorry for my poor English.</p>
  
  <p><strong>Data Structure</strong></p>
  
  <pre><code>"posts" : {
  "-K_FaI8rsasB0hvFNTTv" : {
    "category" : 0, -&gt; int
    "createdDate" : 1483115934775,
    "image" : "https://firebasestorage.googleapis.com/...",
    "status" : 0, -&gt; int
    "title" : "sadfsadfsadf",
    "userId" : "iuHJQ044GrMPjRMF1CxPq15tp6g2",
    "username" : "someone",
    "voteSum" : 34 -&gt; Long
  },
  "-K_GI57zSiPP6ETgctPD" : {
    "category" : 0, -&gt; int
    "createdDate" : 1483127677924,
    "image" : "https://firebasestorage.googleapis.com/...",
    "status" : 0, -&gt;int
    "title" : "qwewqeqwe",
    "userId" : "MWv568D0f3VO1y683kZumOQ7gHZ2",
    "username" : "idiots",
    "voteSum" : 13 -&gt; Long
  }
  
  }
  </code></pre>
  
  <p><strong>FirebaseRecyclerAdapter:</strong></p>
  
  <pre><code>public FirebaseRecyclerAdapter recyclerAdapter(Query query) {
  
      FirebaseRecyclerAdapter&lt;Post, PostViewHolder&gt; firebaseRecyclerAdapter = new FirebaseRecyclerAdapter&lt;Post, PostViewHolder&gt;(
              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;
  }
  </code></pre>