Multiple choice AlertDialog with custom Adapter

女生的网名这么多〃 提交于 2019-11-30 13:11:21

Unfortunately, there doesn't seem to be an easy way to toggle on AlertDialog's multichoicemode without calling setMultiChoiceItems().

However, you can set an adapter, then turn on multichoice mode in the contained ListView itself.

final AlertDialog dialog = new AlertDialog.Builder(getActivity())
    .setTitle("Title")
    .setAdapter(yourAdapter, null)
    .setPositiveButton(getResources().getString(R.string.positive), null)
    .setNegativeButton(getResources().getString(android.R.string.cancel), null)
    .create();

dialog.getListView().setItemsCanFocus(false);
dialog.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
dialog.getListView().setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        // Manage selected items here
        System.out.println("clicked" + position);
        CheckedTextView textView = (CheckedTextView) view;
        if(textView.isChecked()) {

        } else {

        }
    }
});

dialog.show();

this will stop ur dialog from disappearing after one selection.

AlertDialog alertDialog = builder.create();
ListView listView = alertDialog.getListView();
listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        // TODO Auto-generated method stub

    }
});

To get which items are selected , you need to plan your adapter accordingly.

see below code it may help you. i used this in my app.

public static ArrayList<String> Party_list_new = new ArrayList<String>();

ArrayList<String> party_multi_cheked = new ArrayList<String>();

public void show_alert() {

        final Dialog dia = new Dialog(this);
        dia.setContentView(R.layout.alert_);
        dia.setTitle("Select File to import");
        dia.setCancelable(true);

        final ListView list_alert = (ListView) dia
                .findViewById(R.id.alert_list);

        list_alert.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_multiple_choice,
                Party_list_new));

        list_alert.setItemsCanFocus(false);
        list_alert.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        list_alert.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                    long arg3) {

            }
        });

        Button btn = (Button) dia.findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                SparseBooleanArray positions = list_alert
                        .getCheckedItemPositions();
                int j = 0;

                for (int k = 0; k < Party_list_new.size(); k++) {
                    if (positions.get(k)) {

                        party_multi_cheked.add("" + k);


                    }
                }

                dia.dismiss();
            }
        });
        dia.show();

    }

alert_list.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
         <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Select Party" />

        <ListView
            android:id="@+id/alert_list"
            android:layout_width="match_parent" android:padding="5dp"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>

make it right if it is correct.

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