android - 如何为 AlertDialog 创建 setAdapter()

标签 android android-arrayadapter android-alertdialog

我创建了一个扩展DialogCustomDialogBu​​ilder...我想创建方法setApater()..我创建了该部分,但是适配器上的 onClick 方法不起作用..

下面给出了我的customDialogBu​​ilder类。

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(),它类似于AlertDialog中的setAdapter()

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

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;
        }

关于android - 如何为 AlertDialog 创建 setAdapter(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16537537/

相关文章:

android - 将Admob集成到我的Android Studio项目中时遇到一个奇怪的问题

android - 在 Android 中使用 openGL 绘制动态图形

android - 使用 Mailgun API 通过电子邮件发送 otp

android - 帮助使用 Canvas 使对象在 Android 上可触摸

android - 在 Android 中动态添加子项到 ListView

java - Android 中 ArrayAdapter 和 ListAdapter 的区别?

android - 减小 AlertDialog.Builder 组件的字体大小

android - 快速滚动 ListView 时出现问题

java - 方法中的警报对话框 - Android

c# - 如何在 Monodroid 中显示 MessageBox