如何创建一个AlertDialog setAdapter() [英] how Create setAdapter() for a AlertDialog

查看:1531
本文介绍了如何创建一个AlertDialog setAdapter()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建延伸的Dialog CustomDialogBu​​ilder ...我要创建的方法setApater()..我创建一个节,而是适配器上的onClick方法是行不通的。

我customDialogBu​​ilder类如下。

 公共类CustomAlertDialog扩展对话框
{
    公共CustomAlertDialog(上下文C中,int主题){
        超(C,主题);
    }
    公共静态类生成器
    {
        私人上下文的背景下;
        私人字符串称号;
        私人字符串信息;
        私人字符串positiveButtonText;
        私人字符串negativeButtonText;
        私人查看内容查看;
        私人ListAdapter适配器;
        私人的ListView ListView的;
        私人DialogInterface.OnClickListener
                        positiveButtonClickListener,
                        negativeButtonClickListener,adapterListener;
        公共生成器(上下文C)
        {
            上下文= C;
        }
        公共生成器的setTitle(字符串名称)
        {
            this.title =称号;
            返回此;
        }
        公共生成器setMessage(字符串消息){
            this.message =消息;
            返回此;
        }
        公共生成器的setContentView(视图V)
        {
            内容查看= V;
            返回此;
        }
        公共生成器setAdapter(ListAdapter适配器,DialogInterface.OnClickListener听众)
        {
            this.adapter =适配器;
            返回此;
        }
        公共生成器setPositiveButton(字符串positiveButtonText,
                DialogInterface.OnClickListener监听){
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener =侦听器;
            返回此;
        }
        公共生成器setNegativeButton(字符串negativeButtonText,
                DialogInterface.OnClickListener监听){
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener =侦听器;
            返回此;
        }
        公共CustomAlertDialog创建()
        {
            LayoutInflater吹气=(LayoutInflater)上下文
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            最后CustomAlertDialog对话框=新CustomAlertDialog(背景下,
                    R.style.Dialog);
            查看布局= inflater.inflate(R.layout.dialog_title_layout,NULL);
            dialog.addContentView(布局,新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            ((的TextView)layout.findViewById(R.id.tv_custom_dialog_title))的setText(职称)。
            如果(positiveButtonText!= NULL){
                ((按钮)layout.findViewById(R.id.bt_custom_dialog_positive))
                        .setText(positiveButtonText);
                如果(positiveButtonClickListener!= NULL){
                    ((按钮)layout.findViewById(R.id.bt_custom_dialog_positive))
                            .setOnClickListener(新View.OnClickListener(){
                                公共无效的onClick(视图v){
                                    positiveButtonClickListener.onClick(
                                            对话,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            }
            其他
                layout.findViewById(R.id.bt_custom_dialog_positive).setVisibility(
                        View.GONE);
             如果(negativeButtonText!= NULL){
                 ((按钮)layout.findViewById(R.id.bt_custom_dialog_negative))
                         .setText(negativeButtonText);
                 如果(negativeButtonClickListener!= NULL){
                     ((按钮)layout.findViewById(R.id.bt_custom_dialog_negative))
                             .setOnClickListener(新View.OnClickListener(){
                                 公共无效的onClick(视图v){
                                     positiveButtonClickListener.onClick(
                                            对话,
                                             DialogInterface.BUTTON_NEGATIVE);
                                 }
                             });
                 }
             }其他{
                 //如果没有确认键只设置能见度GONE
                 layout.findViewById(R.id.bt_custom_dialog_negative).setVisibility(
                         View.GONE);
             }
             如果(消息!= NULL){
                 ((的TextView)layout.findViewById(
                        R.id.tv_custom_dilaog_message))的setText(消息)。
             }
             否则,如果(适配器!= NULL)
             {
                 ListView控件=新的ListView(背景);
                 listView.setAdapter(适配器);
                 ((的LinearLayout)layout.findViewById(R.id.Layout_custom_dialog_content))
                 .removeAllViews();
         ((的LinearLayout)layout.findViewById(R.id.Layout_custom_dialog_content))
         .addView(ListView控件);
         如果(adapterListener!= NULL)
         {
             listView.setOnItemClickListener(新OnItemClickListener(){                公共无效onItemClick(适配器视图<>为arg0,ARG1查看,
                        INT ARG2,长ARG3){
                }
            });
         }             }
             否则,如果(内容查看!= NULL){
                 //如果没有消息集
                 //的内容查看添加到对话体
                 ((的LinearLayout)layout.findViewById(R.id.Layout_custom_dialog_content))
                         .removeAllViews();
                 ((的LinearLayout)layout.findViewById(R.id.Layout_custom_dialog_content))
                         .addView(内容查看,
                                 新的LayoutParams(
                                         LayoutParams.WRAP_CONTENT,
                                         LayoutParams.WRAP_CONTENT));
             }
            返回对话框;
        }
    }
}

如何才能创建正确的setAdapter(),它类似于setAdapter()在AlertDialog。

这是我如何创建对话框中使用这个类

 对话对话框= NULL;
                    的String []项目= {编辑个人资料,更改医生,更改密码,注销};
                    ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(Loged.this,
                            R.layout.my_spinner_layout,项目);                CustomAlertDialog.Builder customBuilder =新
                        CustomAlertDialog.Builder(Loged.this);
                    customBuilder.setTitle(「购股权」)
                       .setAdapter(适配器,新DialogInterface.OnClickListener(){                        公共无效的onClick(DialogInterface对话,诠释它){
                            Log.e(DIS,+其中);                        }
                    });
                    对话框= customBuilder.create();
                    dialog.show();


解决方案

hureyyy ......得到了答案........

在我们CustomDialog类...为

改变setAdapter功能

 公共生成器setAdapter(ListAdapter适配器,DialogInterface.OnClickListener听众)
        {
            this.adapter =适配器;
            this.adapterListener =侦听器;
            返回此;
        }

i create a CustomDialogBuilder which extends Dialog... i want to create the method setApater().. i create that section but the onClick method on the adapter is not working..

my customDialogBuilder class is given below.

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

how we can create the correct setAdapter() which is similar to setAdapter() in AlertDialog.

this is how i create dialog using this class

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...... got the answer........

change the setAdapter function in our CustomDialog class... as

public Builder setAdapter(ListAdapter adapter,DialogInterface.OnClickListener listener)
        {
            this.adapter=adapter;
            this.adapterListener=listener;
            return this;
        }

这篇关于如何创建一个AlertDialog setAdapter()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆