11

我正在尝试创建一个带有多项选择选项的 AlertDialog。我已经尝试过,setMultiChoiceItems但我拥有的是 aArrayList<Category>而不是 aCharSequence所以我尝试了适配器。

问题setAdapter在于,当我选择一项时,它会关闭对话框窗口。我想要的是选择项目,然后点击确定按钮查看选择了哪些项目。

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Pick a color");
        ArrayAdapter<Category> catsAdapter = new ArrayAdapter<Category>(this, android.R.layout.select_dialog_multichoice,this.categories);
        builder.setAdapter(catsAdapter, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {

            }
        });
        builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                //do something  
               }                
        });;

        AlertDialog alert = builder.create();
        alert.show();
4

3 回答 3

33

不幸的是,似乎没有一种简单的方法可以在不调用setMultiChoiceItems().

ListView但是,您可以设置一个适配器,然后在包含本身中打开多选模式。

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();
于 2013-07-28T10:40:37.517 回答
1

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.

于 2013-06-04T05:22:45.170 回答
0

请参阅下面的代码,它可能会对您有所帮助。我在我的应用程序中使用了这个。

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>

如果它是正确的,让它正确。

于 2012-06-07T14:12:41.967 回答