我有一个使用自定义ArrayAdapter加载到AlertDialog中的图像列表,我使用的是AlertDialog builder.setSingleChoiceItems但它没有显示单选按钮。这是我的一些代码:
final ListAdapter adapter = new IconAdapter(AddNote.this, R.layout.list_image_item);
btnPickIcon.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(AddNote.this);
builder.setTitle("Pick an icon");
builder.setSingleChoiceItems(adapter, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item)
{
Toast.makeText(AddNote.this, "You selected " + item,Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
适配器:
class ViewHolder {
ImageView icon;
}
public class IconAdapter extends ArrayAdapter<Integer> {
// ArrayList<View> imageViews = new ArrayList<View>();
private Integer[] mIconList = {
R.drawable.symbol1, R.drawable.symbol2, R.drawable.symbol3, R.drawable.symbol4, R.drawable.symbol5};
public IconAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
ViewHolder holder;
public int getCount() {
return mIconList.length;
}
public Integer getItem(int position) {
return mIconList[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_image_item, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.listImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Drawable tile = getResources().getDrawable(mIconList[position]);
holder.icon.setImageDrawable(tile);
return convertView;
}
}
任何人都知道为什么收音机按钮没有出现?
答案 0 :(得分:0)
final ListAdapter adapter = new IconAdapter(AddNote.this, R.layout.simple_list_item_single_choice);
使用simple_list_item_single_choice而不是list_image_item。