0

I'm developing an android app and in this app users should be able to search for other users knowing a certain language depending on their preferences. To do that I get the desired language and make a query in the database but I couldn't figure out how to bring values here.

So below you see my database structure in the picture.Say user selected a language called "Italian" and this should return the all users knowing Italian language.

FireBase Database STructure

Here is the code: for example, it should return the users like Ridvan and Fenerbahce if they have a language with Italian.

 DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReference("Users");



    myRootRef.child("Users").orderByChild("Languages").equalTo(Language).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {



        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
theroglu
  • 136
  • 3
  • 12

1 Answers1

0

The equalTo messes you up here, since there is no value Italian. You'll want

myRootRef.child("Users").orderByChild("Languages/Italian").startAt(" ")

The space here is just a character with a very low ASCII value, so that we get all results.

Note that this structure requires that you define an index for each language on the users node:

"Users": {
  ".indexOn": ["Italian", "Spanish", "Polish"] 
}

This may be possible for languages. But if the values are user-generated, this turns into a maintenance problem for the indexes. In that case, consider creating inverted lookups: having a top-level list of languages and the associated users for each. For more on that, see my answer here: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807