0

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!

André Silva
  • 121
  • 11
  • try [this](https://stackoverflow.com/a/7725078/7505436) for generating unique random numbers – vm345 Jul 16 '18 at 21:48
  • I could use `Collections.shuffle(number);` but, for example, if I have an image of Portugal I need, in my nameList, the name "Portugal" too. Using that `Collections.shuffle(number);` it would shuffle all the lists randomly and then I could get the image of Portugal but the name of other country... – André Silva Jul 16 '18 at 21:59
  • ArrayList number = new ArrayList(); for (int i = 1; i <=urlCount ; ++i) number.add(i); int randomNumber = Collections.shuffle(number); – vm345 Jul 16 '18 at 22:16
  • I get this error Incompatible types. Required: int Found: void When trying to do `int randomNumber`... – André Silva Jul 16 '18 at 22:22

0 Answers0