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):
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):
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?