In my CardStackFragment
I have a query that retrieves information on my "paises" child from the Firebase Realtime Database. This basically retrieves 4 things: Image Url
, Country Name
, Country Continent
, Country Spoken Language
for each country that is registered in my DB.
This is the code:
countriesRef.removeEventListener(this);
List<String> urlList = new ArrayList<>();
List<String> nomePaisList = new ArrayList<>();
List<String> continenteList = new ArrayList<>();
List<String> linguaList = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren())
{
String url = ds.child("Imagem").getValue(String.class);
String nomePais = ds.child("Nome").getValue(String.class);
String continente = ds.child("Continente").getValue(String.class);
String lingua = ds.child("Língua").getValue(String.class);
urlList.add(url);
nomePaisList.add(nomePais);
continenteList.add(continente);
linguaList.add(lingua);
}
int urlCount = urlList.size();
int randomNumber = new Random().nextInt(urlCount);
final List<String> randomUrlList = new ArrayList<>();
final List<String> randomNomePaisList = new ArrayList<>();
final List<String> randomContinenteList = new ArrayList<>();
final List<String> randomLinguaList = new ArrayList<>();
for (int i=0; i<=Constants.TOTAL_PAISES; i++)
{
randomUrlList.add(urlList.get(randomNumber));
randomNomePaisList.add(nomePaisList.get(randomNumber));
randomContinenteList.add(continenteList.get(randomNumber));
randomLinguaList.add(linguaList.get(randomNumber));
Picasso.with(getContext()).load(randomUrlList.get(i)).into(imgFotoPais);
txtPaisNome.setText(randomNomePaisList.get(i));
imgFotoPais2.setVisibility(View.VISIBLE);
txtPaisNome.setVisibility(View.VISIBLE); }
}
I then add it to a random list to make appear different countries that appeared last time, when the user refreshes...
The problem is that, my Random
function can generate a number that has been already generated before so in my 5 ImageViews, more than one can have the same image. How can I validate the Random
function to generate only numbers that is hasn't generated previously?
EDIT
Collections.shuffle(number);
is NOT an option!