In my apps preferences sc开发者_运维知识库reen, i want to pop up a dialog that shows a list of checkbox items that are dynamically generated.
How does one do that and also, how does one get the checked values? I have made custom dialogs in the past, but for some reason my brain wont function today ...
Thanks.
The way I've done this is to create a ListView that contains rows of CheckBoxes.
private class CheckBoxListAdapter extends ArrayAdapter<CheckBoxListRowItem> {
}
To get the checked values, I call setOnCheckedChangeListener for each CheckBox. Each time it's checked, it updates the my model data (CheckBoxListRowItem). When you need to figure out which CheckBoxes are checked, you can get it from the model data, not directly from the CheckBox object (which is how I thought it should work originally).
I ended up creating an activity that extended ListActivity. Since I wanted a list of checkboxes (where 0 or more could be selected), in my
onCreate():
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
and I listen for clicks by overriding onListItemClick().
The list adapter that I used was ArrayAdapter:
setListAdapter(
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
some_string_array));
精彩评论