我正在做的應用程序,我需要在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