我有两个数组和两个ImageView
。我想将数组设置为各自ImageView
中的图像。条件,使用循环或否则将图像设置为imageview。两个阵列都有2个图像。
答案 0 :(得分:0)
//第一个数组列表
ArrayList<Integer> f1Child1=new ArrayList<Integer>();
f1Child1.add(R.drawable.animals);
f1Child1.add(0);
flags1.add(f1Child1);
ArrayList<Integer> f1Child2=new ArrayList<Integer>();
f1Child2.add(R.drawable.birds);
f1Child2.add(1);
flags1.add(f1Child2);
//第二阵列列表
ArrayList<Integer> f2Child1=new ArrayList<Integer>();
f2Child1.add(R.drawable.birds);
f2Child1.add(1);
flags2.add(f2Child1);
ArrayList<Integer> f2Child2=new ArrayList<Integer>();
f2Child2.add(R.drawable.aquatic);
f2Child2.add(0);
flags2.add(f2Child2);
listOfFlags.add(flags1);
listOfFlags.add(flags2);
final List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
final String[] from = { "flag"};
final int[] to = { R.id.flag};
//加载第一个网格视图
final GridView gridview = (GridView) findViewById(R.id.gridview1);
for(int i=0;i<listOfFlags.get(0).size();i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("flag", Integer.toString(listOfFlags.get(0).get(i).get(0)));
flags1.get(i);
aList.add(hm);
}
//使用简单的适配器设置gridview
SimpleAdapter simpleAdapter = new SimpleAdapter(getBaseContext(), aList, R.layout.gridviewdata, from, to);
gridview.setAdapter(simpleAdapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {
aList.clear();
//使用弹出窗口或对话框
if (listOfFlags.get(increament).get(position).get(1) == 1) {
ViewDialog alert = new ViewDialog();
alert.showDialog(BirdQuiz.this, "");
correct += 1;
result += 100;
}
else {
ViewDialog1 alert1 = new ViewDialog1();
alert1.showDialog(BirdQuiz.this, "");
wrong +=1;
}
//如果需要传递意图值
if (increament < listOfFlags.size()-1) {
increament++;
}
else{
Intent resultAl = new Intent(BirdQuiz.this, Result.class);
resultAl.putExtra("Correct", correct);
resultAl.putExtra("Result", result);
resultAl.putExtra("Wrong", wrong);
startActivity(resultAl);
finish();
}
//点击后加载下一个gridview
for (int i = 0; i < listOfFlags.get(increament).size(); i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("flag", Integer.toString(listOfFlags.get(increament).get(i).get(0)));
aList.add(hm);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(getBaseContext(), aList, R.layout.gridviewdata, from, to);
gridview.setAdapter(simpleAdapter);
}
});