I just needed a super easy solution for a fixed set of images in a spinner, so I did this:
public class SimpleImageArrayAdapter extends ArrayAdapter<Integer> {
private Integer[] images;
public SimpleImageArrayAdapter(Context context, Integer[] images) {
super(context, android.R.layout.simple_spinner_item, images);
this.images = images;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getImageForPosition(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getImageForPosition(position);
}
private View getImageForPosition(int position) {
ImageView imageView = new ImageView(getContext());
imageView.setBackgroundResource(images[position]);
imageView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
return imageView;
}
}
And then in the code you can just use it like this:
SimpleImageArrayAdapter adapter = new SimpleImageArrayAdapter(context,
new Integer[]{R.drawable.smiley1, R.drawable.smiley2, R.drawable.smiley3, R.drawable.smiley4, R.drawable.smiley5});
spinner.setAdapter(adapter);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…