Android AlertDialog 中的手动 select 选项

Manually select option in Android AlerDialog

我有一个带有多个 selection 的 AlertDialog,它有一个默认的 "All" 选项,如果所有其他选项都被取消select,这个选项将是 select 在对话框仍然打开时编辑。

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

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
        }
    }
})

有办法吗?

我也研究过这个问题,发现如果不使用自定义适配器就无法解决这个问题。在有效的代码下方。在这里,我手动创建了 ListView,并为其设置了自定义适配器。然后在每个项目上单击检查 selected 项目。如果没有项目 selected,我会手动 select 一个默认项目。

@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);
    }
}