Multiple choice alert dialog with custom row layout

微笑、不失礼 提交于 2019-12-21 20:19:12

问题


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


回答1:


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.




回答2:


I would recommend that you create your own dialog class like this:

Customizing dialog by extending Dialog or AlertDialog

How to create a Custom Dialog box in android?

This way you will have full control over your dialog and you can customize it any way you want.

Also if you still have issues with your list view after that you can customize your list view items completely: (You can only affect the background and text in a small way through xml and selectors without doing your custom implementation)

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

Try it out, it may seem hard but when you do it once it will be piece of cake and will do wonders for you in your future development projects.



来源:https://stackoverflow.com/questions/19281945/multiple-choice-alert-dialog-with-custom-row-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!