将 AlertDialog 与自定义适配器一起使用时,单选按钮不显示
我使用自定义 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;
}
}
有人知道为什么单选按钮没有显示吗?
I have a list of images loaded into an AlertDialog using a custom ArrayAdapter and I'm using AlertDialog builder.setSingleChoiceItems but its not displaying the Radio buttons. Here's some of my code:
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();
}
});
And the adapter:
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;
}
}
Anyone know why the radio buttons aren't showing up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 simple_list_item_single_choice 而不是 list_image_item。
Use simple_list_item_single_choice instead of list_image_item.