如何为 AlertDialog 创建 setAdapter()

2024-02-20

我创建了一个CustomDialogBuilder延伸Dialog...我想创建方法setApater()..我创建了该部分,但是onClick适配器上的方法不起作用..

My customDialogBuilder下面给出了类。

public class CustomAlertDialog extends Dialog 
{
    public CustomAlertDialog(Context c, int theme) {
        super(c, theme);
    }
    public static class Builder 
    {
        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private ListAdapter adapter;
        private  ListView listView;
        private DialogInterface.OnClickListener 
                        positiveButtonClickListener,
                        negativeButtonClickListener,adapterListener;
        public Builder(Context c)
        {
            context =c;
        }
        public Builder setTitle(String title)
        {
            this.title =title;
            return this;
        }
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }
        public Builder setContentView(View v)
        {
            contentView =v;
            return this;
        }
        public Builder setAdapter(ListAdapter adapter,DialogInterface.OnClickListener listener)
        {
            this.adapter=adapter;
            return this;
        }
        public Builder setPositiveButton(String positiveButtonText,
                DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }
        public Builder setNegativeButton(String negativeButtonText,
                DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }
        public CustomAlertDialog create()
        {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomAlertDialog dialog = new CustomAlertDialog(context, 
                    R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_title_layout, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            ((TextView) layout.findViewById(R.id.tv_custom_dialog_title)).setText(title);
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.bt_custom_dialog_positive))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.bt_custom_dialog_positive))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(
                                            dialog, 
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            }
            else
                layout.findViewById(R.id.bt_custom_dialog_positive).setVisibility(
                        View.GONE);
             if (negativeButtonText != null) {
                 ((Button) layout.findViewById(R.id.bt_custom_dialog_negative))
                         .setText(negativeButtonText);
                 if (negativeButtonClickListener != null) {
                     ((Button) layout.findViewById(R.id.bt_custom_dialog_negative))
                             .setOnClickListener(new View.OnClickListener() {
                                 public void onClick(View v) {
                                     positiveButtonClickListener.onClick(
                                            dialog, 
                                             DialogInterface.BUTTON_NEGATIVE);
                                 }
                             });
                 }
             } else {
                 // if no confirm button just set the visibility to GONE
                 layout.findViewById(R.id.bt_custom_dialog_negative).setVisibility(
                         View.GONE);
             }
             if (message != null) {
                 ((TextView) layout.findViewById(
                        R.id.tv_custom_dilaog_message)).setText(message);
             }
             else if(adapter!=null)
             {
                 listView = new ListView(context);
                 listView.setAdapter(adapter);
                 ((LinearLayout) layout.findViewById(R.id.Layout_custom_dialog_content))
                 .removeAllViews();
         ((LinearLayout) layout.findViewById(R.id.Layout_custom_dialog_content))
         .addView(listView);
         if(adapterListener!=null)
         {
             listView.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {


                }
            });
         }

             }
             else if (contentView != null) {
                 // if no message set
                 // add the contentView to the dialog body
                 ((LinearLayout) layout.findViewById(R.id.Layout_custom_dialog_content))
                         .removeAllViews();
                 ((LinearLayout) layout.findViewById(R.id.Layout_custom_dialog_content))
                         .addView(contentView, 
                                 new LayoutParams(
                                         LayoutParams.WRAP_CONTENT, 
                                         LayoutParams.WRAP_CONTENT));
             }
            return dialog;
        }
    }
}

我们如何才能创造出正确的setAdapter()这类似于setAdapter() in AlertDialog.

这就是我使用此类创建对话框的方法:

Dialog dialog = null;
                    String[] items = {"Edit profile","Change doctor","Change password","Logout"};
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(Loged.this,
                            R.layout.my_spinner_layout, items);

                CustomAlertDialog.Builder customBuilder = new
                        CustomAlertDialog.Builder(Loged.this);
                    customBuilder.setTitle("Options")
                       .setAdapter(adapter, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            Log.e("dis",""+which);

                        }
                    });
                    dialog = customBuilder.create();
                    dialog.show();

hureyyy……得到答案了……

将我们的 CustomDialog 类中的 setAdapter 函数更改为

public Builder setAdapter(ListAdapter adapter,DialogInterface.OnClickListener listener)
        {
            this.adapter=adapter;
            this.adapterListener=listener;
            return this;
        }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何为 AlertDialog 创建 setAdapter() 的相关文章

随机推荐

  • d3.js v5 - Promise.all 替换 d3.queue

    我已经使用 d3 js v4 一段时间了 我了解到 Mike Bostock 已将 v5 版本中的 d3 queue 替换为 Promise 原生 JavaScript 对象 我想与您核实一下我编写的这段代码是否正确地 异步 这些 URL
  • Java 中 Date(String s) 的未弃用的完全等效项?

    我有旧代码使用new Date dateString 解析日期字符串 编译代码会产生弃用警告Date java lang String in java util Date has been deprecated javadoc无益地建议我使
  • 带有 lxml 子路径的 XPath 谓词?

    我试图理解发送给我的用于 ACORD XML 表单 保险中的常见格式 的 XPath 他们发给我的 XPath 是 为了简洁而被截断 PersApplicationInfo InsuredOrPrincipal InsuredOrPrinc
  • Hudson -CI 屏幕保护程序设置

    您好 有没有我可以设置一个屏幕保护程序 其中包含在 hudson 中运行的项目列表 该列表指示项目的状态 假设屏幕保护程序的部分表示项目成功 则显示绿色 如果项目构建失败 则显示红色 可能屏幕保护程序必须分区到多个项目 您可以在任何合适的环
  • 将一个时间序列分割为另一个不规则时间序列

    我正在尝试用一个独特的不规则时间序列分割多个 xts 对象 split xts按天 分钟 秒等进行分割 使用断点需要相等长度的向量 当我尝试分割数据时 这会产生错误 dd lt c 2014 02 23 2014 03 12 2014 05
  • 如何正确地将 IsPressed 属性绑定到我的命令参数?

    我制作了一个自定义按钮来将命令绑定到 自定义 路由 IsPressedChanged事件 以便在按下按钮和释放按钮时执行该命令
  • Python - TypeError:“int64”类型的对象不可 JSON 序列化

    我有一个存储商店名称和每日销售额的数据框 我正在尝试使用下面的 Python 脚本将其插入到 Salesforce 但是 我收到以下错误 TypeError Object of type int64 is not JSON serializ
  • Windows 10 上的 VersionNT MSI 属性

    我发现当我更新引导程序的清单以支持 Windows 10 兼容性时 MSI 的 InstallUISequence 将正确设置 VersionNT 1000 但 InstallExecuteSequence 将设置 VersionNT 60
  • 将 UserID 从 ASP.Net 安全地传递到 Javascript

    在我当前正在开发的应用程序中 我们使用 ASP Net 表单身份验证来授予用户对站点的进一步访问权限 该网站面向移动用户 因此我们试图尽可能摆脱服务器的束缚 并利用 KnockoutJS 进行 Web 服务调用并加载数据 以便用户可以查看它
  • Azure 存储将 blob 移动到其他容器

    我正在寻找一种将 Azure 中的 blob 从一个容器移动到另一个容器的方法 我找到的唯一解决方案是使用 Azure 存储数据移动库 但这似乎适用于不同帐户 我想将同一帐户内的 blob 移动到另一个容器 我没用过Azure Storag
  • JUnit。并行运行。但所有测试方法都处理单例实例。怎么解决?

    所以 我有几个JUnit类 每个类都包含一个测试方法列表 每个方法都是相互独立的 没有直接的联系 但我们有间接联系 所有方法都处理一个单例对象 它是Selenium Web Driver实例 是的 我用1Web Driver我所有测试的实例
  • 如何优雅地结束 spring @Schedule 任务?

    我正在尝试让 Spring Boot 服务优雅地结束 它有一个方法 Scheduled注解 该服务使用 spring data 作为数据库 使用 spring cloud stream 作为 RabbitMQ 在计划的方法结束之前 数据库和
  • CSS,如何使水平菜单和子菜单居中?

    我正在学习 css 但我不知道菜单和子菜单居中 我正在使用 margin auto 或 margin left 和 margin right 为 auto 但它不起作用 任何帮助 将不胜感激 谢谢 HTML div ul li a href
  • 如何在JavaScript中不使用输入类型文件读取包含html的文本文件?

    我有一个文本文件assets包含一些要在特定组件的 div 中呈现的 html 的文件夹 有没有一种方法可以读取该文件并将内容分配给字符串变量 而无需用户与 ngOnInit 中的视图 具有输入文件类型 进行交互 我的发现如果我将 html
  • 如何覆盖 CSS

    我有基本的 CSS 经验 所以我想知道如何删除由 Primefaces 设置的 CSS 样式 ui inputfield ui widget content ui inputfield ui widget header ui inputfi
  • Hadoop:映射器和缩减器的数量

    我使用不同数量的映射器和缩减器 例如 1 个映射器和 1 个缩减器 1 个映射器和 2 个缩减器 1 个映射器和 4 个缩减器 在 1 1GB 文件上多次运行 Hadoop MapReduce Hadoop安装在具有超线程的四核机器上 以下
  • JS:未捕获类型错误:对象不是函数(onclick)

    Edit 这是一个 JSfiddle http jsfiddle net XpmZG Edit2 错误在这一行
  • 如何获取物理设备支持的音频格式(WinAPI、Windows)

    我有一个音频设备 USB 麦克风 我想找出它本机支持的音频格式 位深度和采样率 在 OS X 上有一个很好的 kAudioStreamPropertyAvailablePhysicalFormats Core Audio 属性 但我找不到类
  • 使用文件名中的当前日期创建文件 + Robocopy 日志记录

    由于对批处理文件 脚本和一般 编码 的经验很少 我很快就遇到了我想要创建的批处理的问题 情况如下 我有一个文件夹 其中自动插入 txt 文件 我想根据文件的名称将这些文件移动到不同的文件夹 我用 Robocopy 做了这个 效果很好 然后我
  • 如何为 AlertDialog 创建 setAdapter()

    我创建了一个CustomDialogBuilder延伸Dialog 我想创建方法setApater 我创建了该部分 但是onClick适配器上的方法不起作用 My customDialogBuilder下面给出了类 public class