3

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

4

2 回答 2

5

好吧,我知道我应该创建一个自定义对话框,但现在我没有时间去做......所以这就是我解决这个问题的方法:

        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();

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

于 2013-10-10T18:04:14.953 回答
0

我建议您像这样创建自己的对话框类:

通过扩展 Dialog 或 AlertDialog 自定义对话框

如何在android中创建自定义对话框?

这样你就可以完全控制你的对话,你可以按照你想要的任何方式自定义它。

此外,如果之后您的列表视图仍有问题,您可以完全自定义列表视图项目:(您只能通过 xml 和选择器以小的方式影响背景和文本,而无需进行自定义实现)

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

尝试一下,这可能看起来很难,但是一旦你这样做,它就会变得轻而易举,并且会在你未来的开发项目中为你创造奇迹。

于 2013-10-09T20:44:33.280 回答