0

In my app, I have a fragment that allows the user to make a filtered search of the countries he want to see. It looks like this (The user selects the continents of the countries he wants to see and then all the countries of those continents appear):

enter image description here

I'm using Firebase Realtime database in order to save all the countries with their names and an image of each one. This is what my JSON tree looks like (not all countries displayed here): (Btw, I'm sorry because it is all in Portuguese):

enter image description here

I currently can display all countries of a specific continent but, if the user selects more than one continent I can't query all the countries... This is the code I currently have to do that:

 DatabaseReference paisNomeContinentes = mDatabase.child("continentes").child(Constants.PAISES_EUROPA).child("Paises");             // C1 - ÁFRIA        (Constants.PAISES_AFRICA)
    ValueEventListener valueEventListener1 = new ValueEventListener() {                                                          // C2 - ÁSIA         (Constants.PAISES_ASIA)
        @Override                                                                                                               // C3 - ANTÁRTIDA    (Constants.PAISES_ANTARTIDA)
        public void onDataChange(DataSnapshot dataSnapshot) {                                                                   // C4 - AMÉRICA      (Constants.PAISES_AMERICA)
            List<String> nomePaisList = new ArrayList<>();                                                                     // C5 - EUROPA       (Constants.PAISES_EUROPA)
                                                                                                                               // C6 - OCEANIA      (Constants.PAISES_OCEANIA)
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String nomePais = ds.child("Nome").getValue(String.class);
                nomePaisList.add(nomePais);
            }

            int urlCount = nomePaisList.size();
            int randomNumber = new Random().nextInt(urlCount);
            List<String> randomNomePaisList = new ArrayList<>();
            for (int i=0; i<=9; i++) {
                randomNomePaisList.add(nomePaisList.get(randomNumber));
                txtHomeDesc.setText(randomNomePaisList.get(i)); // Inserir na TextView o nome do respetivo país
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    paisNomeContinentes.addListenerForSingleValueEvent(valueEventListener1);

How can I retrieve all the countries of the continents the user has selected?

André Silva
  • 121
  • 11
  • 2
    First thing to come to my mind is having multiple queries (one for each selected continent) and combining the results. – André Kool Jul 05 '18 at 10:07
  • @AndréKool I thought of that too, I just don't know if that is the easiest/best way to do this ahah – André Silva Jul 05 '18 at 10:11
  • 1
    Seeing you already have it working for a single continent and there isn't something like an OR query in firebase realtime database I think this will be the easiest sollution (for you). You can also take a look at [this question](https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase) for some more ideas about querying firebase. – André Kool Jul 05 '18 at 10:17
  • Actually that code comes from my other test of structuring the Firebase Realtime Database. This is the structure I use for that code to work: https://imgur.com/a/UA1YayY How can I, based on the structure that I've got in my question, can retrieve information for each continent? – André Silva Jul 05 '18 at 13:16

1 Answers1

0

There can be mutiple ways to achieve this. One of them is done below-

String nomePais;
String nome;
List<String> nomePaisList;
DatabaseReference paisNomeContinentes = mDatabase.child("Continente").child(Constants.PAISES_EUROPA).child("Paises");             // C1 - ÁFRIA        (Constants.PAISES_AFRICA)
    ValueEventListener valueEventListener1 = new ValueEventListener() {                                                          // C2 - ÁSIA         (Constants.PAISES_ASIA)
        @Override                                                                                                               // C3 - ANTÁRTIDA    (Constants.PAISES_ANTARTIDA)
        public void onDataChange(DataSnapshot dataSnapshot) {                                                                   // C4 - AMÉRICA      (Constants.PAISES_AMERICA)
                                                                               // C5 - EUROPA       (Constants.PAISES_EUROPA)
                                                                                                                               // C6 - OCEANIA      (Constants.PAISES_OCEANIA)
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                nomePais = ds.child("Continente").getValue(String.class);
                if(nomePais.equals("AMERICA") || nomePais.equals("EUROPIA") {
                    nome = nomePais;
                    fetch(nome);
                 } // Suppose user selects continent AMERICA & EUROPIA
            }

void fetch(String n) {
   Query paisNome = mDatabase.child("continentes").child(Constants.PAISES_EUROPA).child("Paises").child("P;             1").child("Nome").equalTo(n);
   ValueEventListener valueEventListener1 = new ValueEventListener() {                                                          
    @Override                                                                                                               
    public void onDataChange(DataSnapshot dataSnapshot) {                                                                   
        nomePaisList = new ArrayList<>();                                                                     
        nome = dataSnapshots.getValue().toString();
        nomePaisList.add(nome);

    }
}

I hope this helps ....

Raj
  • 2,997
  • 2
  • 12
  • 30