在我的CardStackFragment
中,我有一个查询,该查询从 Firebase实时数据库检索有关我的“赔付”子级的信息。这基本上检索了 4 项:Image Url
,Country Name
,Country Continent
,Country Spoken Language
,用于在数据库中注册的每个国家。
这是代码:
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); }
}
然后我将其添加到随机列表中,以使用户刷新时出现上次出现的不同国家/地区...
问题在于,我的Random
函数可以生成一个之前已经生成的数字,因此在我的5个ImageView中,可以有多个图像具有相同的图像。如何验证Random
函数以仅生成以前未生成的数字?
编辑
Collections.shuffle(number);
是不的选择!