将 AlertDialog 与自定义适配器一起使用时,单选按钮不显示

发布于 2024-11-08 05:20:02 字数 2571 浏览 3 评论 0原文

我使用自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2024-11-15 05:20:02
final ListAdapter adapter = new IconAdapter(AddNote.this, R.layout.simple_list_item_single_choice);

使用 simple_list_item_single_choice 而不是 list_image_item。

final ListAdapter adapter = new IconAdapter(AddNote.this, R.layout.simple_list_item_single_choice);

Use simple_list_item_single_choice instead of list_image_item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文