Skip to content Skip to sidebar Skip to footer

Multiple Choice Alertdialog With Custom Adapter

I am trying to create a AlertDialog with multiple choice option. I have tried with the setMultiChoiceItems but what i have is a ArrayList and not a CharSequence so

Solution 1:

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.

finalAlertDialogdialog=newAlertDialog.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(newOnItemClickListener() {
    @OverridepublicvoidonItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        // Manage selected items here
        System.out.println("clicked" + position);
        CheckedTextViewtextView= (CheckedTextView) view;
        if(textView.isChecked()) {

        } else {

        }
    }
});

dialog.show();

Solution 2:

this will stop ur dialog from disappearing after one selection.

AlertDialogalertDialog= builder.create();
ListViewlistView= alertDialog.getListView();
listView.setOnItemClickListener(newOnItemClickListener() {

    @OverridepublicvoidonItemClick(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.

Solution 3:

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

publicstatic ArrayList<String> Party_list_new = newArrayList<String>();

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

publicvoidshow_alert() {

        finalDialogdia=newDialog(this);
        dia.setContentView(R.layout.alert_);
        dia.setTitle("Select File to import");
        dia.setCancelable(true);

        finalListViewlist_alert= (ListView) dia
                .findViewById(R.id.alert_list);

        list_alert.setAdapter(newArrayAdapter<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(newOnItemClickListener() {

            publicvoidonItemClick(AdapterView<?> arg0, View arg1, int pos,
                    long arg3) {

            }
        });

        Buttonbtn= (Button) dia.findViewById(R.id.button1);
        btn.setOnClickListener(newOnClickListener() {
            publicvoidonClick(View v) {

                SparseBooleanArraypositions= list_alert
                        .getCheckedItemPositions();
                intj=0;

                for (intk=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"?><LinearLayoutxmlns: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" ><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Select Party" /><ListViewandroid: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.

Post a Comment for "Multiple Choice Alertdialog With Custom Adapter"