jnapor jnapor - 24 days ago 12
Android Question

What should be my query to get a list of nodes in firebase

Data Structure

What should be my query be if I want to get the list of all records which gender = 0 and userType = 1? I'm quite new to firebase and I can't find any sources that teaches compound some queries.

Answer

first of all create a node name userList and add instances of user with key 0,1,2,3 and so on like in screen shot in question. It becomes an array of userList

List<User> userList = new ArrayList<>();
Firebase ref = new Firebase(FIREBASE_URL);

  ref.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot snapshot) {
          for (DataSnapshot postSnapshot: snapshot.getChildren()) {
            <User> user = postSnapshot.getValue(<User>.class);
            userList .add(user);
          }
      }
      @Override
      public void onCancelled(FirebaseError firebaseError) {
          Log.e("The read failed: " ,firebaseError.getMessage());
      }
  });

And you can also use Firebase RecyclerAdapter like this

Firebase is no SQL. it doesn't have where clause. You can use orderBy,startAt,endAt function to achieve filtering . See this and this.

Filter most on the server, do the rest on the client

 orderBy('gender')
  .startAt('0').endAt('0')

and in onDataChange

  for (DataSnapshot postSnapshot: snapshot.getChildren()) 
  {
       <User> user = postSnapshot.getValue(<User>.class);
       if(user.getUserType == 1) 
           userList .add(user);
  }
Comments