我正在做的应用程序,我需要在gridview中显示一些图像。我需要随意在该GridView中显示图像。所以在这里我采取了一个像mThumbIds[]
这样的整数数组,我添加了所有图像,然后我使用arraylistl,例如solutionList
,然后是mThumbIds [] intrgerarray。如何将随机整数数组添加到gridview android
我加入到solutionList array.then我应用随机函数,该solutionList arrat然后agaain我加入solutionList数组到像一个整数数组像“randomNumbers []”然后最后我添加randomLumbers []数组gridview我得到随机图像但我的探头是在gridview重新打印的图像来了,但我不想重复的图像。在mThumbIds []我没有给予repted images.please任何人建议我。
ImageAdapter .class:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
// TODO Auto-generated constructor stub
mContext = c;
}
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
int unique=0;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Collections.shuffle(solutionList);
Integer[] randomNumbers=(Integer[])solutionList.toArray();
imageView.setImageResource(randomNumbers[unique]);
unique++;
return imageView;
}
private Integer[] mThumbIds={R.drawable.a,R.drawable.bb,R.drawable.cc,R.drawable.dd,R.drawable.ee,R.drawable.ff,R.drawable.galley,R.drawable.gg};
List<Integer> solutionList = Arrays.asList(mThumbIds);
}
thakyou for response.but我得到java.lang.UnsupportedOperationException usedIndexes.add(index);行 – user1105975 2012-03-19 13:44:07
@ user1105975,这很奇怪,因为ArrayList.add()方法不会抛出这个异常。您正在使用我提供的代码吗? – Egor 2012-03-19 13:50:49
private Integer [] mThumbIds = {R.drawable.a,R.drawable.bb,R.drawable.cc,R.drawable.dd};列表 solutionList = Arrays.asList(mThumbIds); int index; \t do { \t index = new Random()。nextInt(mThumbIds.length); \t} while(solutionList.contains(index)); \t \t Integer [] randomNumbers =(Integer [])solutionList.toArray(); \t \t imageView.setImageResource(randomNumbers [index]); \t \t solutionList.add(index);我是用这种方式写的。 –
user1105975
2012-03-19 13:53:07