在Android AlertDialog中手动选择选项

时间:2015-02-15 19:07:26

标签: android alertdialog multi-select

我有一个多选的AlertDialog,它有一个默认的" All"选项,如果取消选中所有其他选项,则在对话框仍处于打开状态时将选择此选项。

但是,我不确定如何手动选择默认选项。目前我有

List<T> selection = new ArrayList<>();
builder.setMultiChoiceItems(optionLabels, selectedArray, new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        if (isChecked) {
            selection.add(options.get(which));
        } else {
            selection.remove(options.get(which));
        }

        if (selection.isEmpty()) {
            //manually select the default option here
        }
    }
})

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:1)

我也研究过这个问题,但如果不使用自定义适配器,就无法实现这一目标。下面的代码有效。这里我手动创建ListView,并为它设置自定义适配器。然后在每个项目上单击检查所选项目。如果没有选择项目,我会手动选择默认项目。

@NonNull
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
    ListView modeList = new ListView(getActivity());
    MySimpleAdapter adapter = new MySimpleAdapter(getActivity());
    modeList.setAdapter(adapter);
    modeList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    adapter.addAll(optionLabels);
    modeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
            SparseBooleanArray selected = ((ListView) parent).getCheckedItemPositions();
            if (selected == null) {
                // That means our list is not able to handle selection
                // (choiceMode is CHOICE_MODE_NONE for example)
                return;
            }

            for (int i = 0; i < selected.size(); i++) {
                // This tells us the item position we are looking at
                int key = selected.keyAt(i);
                // And this tells us the item status at the above position
                boolean isChecked = selected.get(key);
                // And we can get our data from the adapter like that
                String item = (String) parent.getItemAtPosition(key);
                if (isChecked) {
                    selection.add(item);
                } else {
                    selection.remove(item);
                }
            }

            // Nothing is selected
            if (selection.isEmpty()) {
                ((ListView)parent).setItemChecked(positionOfDefaultItem,                true); // <-- Position of default option here
                String item = (String) parent.getItemAtPosition(positionOfDefaultItem);
                selection.add(item);
            }
        }
    });

    // Use the Builder class for convenient dialog construction
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(modeList)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    _timePickerListener.onTimePeriodPicked(DialogSelectTime.this, _selectedPeriods);
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // Do nothing. Dialog is closed automatically
                }
            });

    // Create the AlertDialog object and return it
    return builder.create();
}

private class MySimpleAdapter extends ArrayAdapter<String> {

    public MySimpleAdapter(final Context context) {
        super(context, android.R.layout.simple_list_item_multiple_choice);
    }
}