I am doing application and I need to dispaly some images in a gridview. I need to display images in that gridview randomly. So here I taken one integer array like mThumbIds[] in that I add all images, then I take arraylistl like solutionList then that mThumbIds[] intrgerarray.
I added into solutionList array.then i apply random function to that solutionList arrat then agaain i add solutionList array into one integer array like “randomNumbers[]” then finally i add that randomNumbers[] array to gridview i get random images but my probem is in gridview repated images coming,but i dont want repeted images. in mThumbIds[] i am not given repted images.please any one suggest me.
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);
}
A solution here is to store indexes of items that are already used in a special
ArrayList. And also I’d recommend you using thejava.util.Randomclass instead of shuffling the array everytime. The code will look like this:Hope this helps.