与自定义行布局多选警报对话框 [英] Multiple choice alert dialog with custom row layout

查看:145
本文介绍了与自定义行布局多选警报对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要与多项选择题创建AlertDialog但我遇到了一些麻烦试图自定义布局文件设置为内​​部的ListView。

有关单项选择的项目我使用一个构造函数一个ListAdapter作为参数,这样我可以设置每行的适当布局资源:

  builder.setSingleChoiceItems(新ArrayAdapter<串GT;(getActivity()
                R.layout.list_item_single_choice_answer,物品),checkedItem,
                新DialogInterface.OnClickListener(){                    @覆盖
                    公共无效的onClick(DialogInterface对话,诠释它){
                        checkedItem =其中;
                        toggleEditTextAnswer(checkedItem ==(items.length - 1));
                        dialog.dismiss();
                    }
                });

问题是,有没有为构造的 setMultiChoiceItems 创建多重选择列表时,接受一个ListAdapter作为参数。<​​/ P>

我需要为每行设置自定义布局,因为我用绘选择器设置行背景和文本颜色。

任何想法?

PS。这里是AlertDialog源$ C ​​$ C以获取更多信息。
<一href=\"https://android.googlesource.com/platform/frameworks/base.git/+/android-4.2.2_r1/core/java/android/app/AlertDialog.java\" rel=\"nofollow\">https://android.googlesource.com/platform/frameworks/base.git/+/android-4.2.2_r1/core/java/android/app/AlertDialog.java


解决方案

好了,我知道我应该创建一个自定义对话框,但现在我没有时间去做......所以这是我怎么黑了这问题:

  AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());
        //设置适配器
        builder.setAdapter(
                新ArrayAdapter&LT;串GT;(getActivity()
                        R.layout.list_item_multiple_choice_answer,物品),NULL)
        //设置操作按钮
                .setPositiveButton(android.R.string.ok,
                        新DialogInterface.OnClickListener(){
                            @覆盖
                            公共无效的onClick(DialogInterface对话,诠释的id){
                                dialog.dismiss();
                            }
                        });        AlertDialog alertDialog = builder.create();        ListView控件的ListView = alertDialog.getListView();
        listView.setAdapter(新ArrayAdapter&LT;串GT;(getActivity()
                R.layout.list_item_multiple_choice_answer,物品));
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setOnItemClickListener(新AdapterView.OnItemClickListener(){            @覆盖
            公共无效onItemClick(适配器视图&LT;&GT;母公司,观景,INT位置,长的id){
                CheckedTextView checkedTextView =(CheckedTextView)视图。
                checkedItems [位置] = checkedTextView.isChecked()!;
            }
        });
        listView.setDivider(NULL);
        listView.setDividerHeight(-1);        alertDialog.setOnShowListener(新DialogInterface.OnShowListener(){            @覆盖
            公共无效昂秀(DialogInterface对话){
                setCheckedItems(((AlertDialog)对话框).getListView());
            }
        });        alertDialog.show();

首先,我设置适配器的项目和而不是调用setMultiChoiceItems我从对话取得的ListView对象,然后配置它自己。

I need to create an AlertDialog with multiple choice items but I'm having some trouble trying to set a custom layout file to the internal ListView.

For single choice items I use a constructor that takes a ListAdapter as parameter and this way I can set the proper layout resource for each row:

        builder.setSingleChoiceItems(new ArrayAdapter<String>(getActivity(),
                R.layout.list_item_single_choice_answer, items), checkedItem,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        checkedItem = which;
                        toggleEditTextAnswer(checkedItem == (items.length - 1));
                        dialog.dismiss();
                    }
                });

The problem is that there's no constructor for setMultiChoiceItems that accepts a ListAdapter as parameter when creating a multiple choice list.

I need to set a custom layout for each row because I use drawable selectors for setting the row background and text color.

Any ideas?

PS. here is the AlertDialog source code for more information. https://android.googlesource.com/platform/frameworks/base.git/+/android-4.2.2_r1/core/java/android/app/AlertDialog.java

解决方案

Well, I know I should create a custom Dialog but right now I don't have the time to do it ... so this is how I hacked this problem:

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Set the adapter
        builder.setAdapter(
                new ArrayAdapter<String>(getActivity(),
                        R.layout.list_item_multiple_choice_answer, items), null)
        // Set the action buttons
                .setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }
                        });

        AlertDialog alertDialog = builder.create();

        ListView listView = alertDialog.getListView();
        listView.setAdapter(new ArrayAdapter<String>(getActivity(),
                R.layout.list_item_multiple_choice_answer, items));
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CheckedTextView checkedTextView = (CheckedTextView) view;
                checkedItems[position] = !checkedTextView.isChecked();
            }
        });
        listView.setDivider(null);
        listView.setDividerHeight(-1);

        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {
                setCheckedItems(((AlertDialog) dialog).getListView());
            }
        });

        alertDialog.show();

First I set the adapter with the items and the instead of calling setMultiChoiceItems I get the ListView object from the Dialog and then configure it myself.

这篇关于与自定义行布局多选警报对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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