0

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.

jnapor
  • 63
  • 1
  • 1
  • 9
  • use addListenerForSingleValueEvent like http://stackoverflow.com/a/36611301/3496570 – Zar E Ahmer Nov 09 '16 at 05:16
  • but you should better create array of user class with key 0 ,1,2 and so on. And read list of User – Zar E Ahmer Nov 09 '16 at 05:36
  • So what you are trying to imply is that I'll get the list of users then filter it one by one programmatically – jnapor Nov 09 '16 at 06:27
  • firebase have function for it . Use startAt ,endAt and orderBy for querying data like http://stackoverflow.com/a/26701282/3496570 and http://stackoverflow.com/a/37952868/3496570 – Zar E Ahmer Nov 09 '16 at 06:32

1 Answers1

2

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);
  }
Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300