The way that @Rahul Gupta suggested works after implementing a ListView
and OnItemClickListener
, but if the ListView
contains too many items, the Views
that are not shown are going to be "checked" as well. I suppose the Views
are not yet generated until the user actually scrolls down and see the rest of the items.
The way that I made it work was creating a Layout
with a single ListView
and setting the value of choice mode
to CHOICE_MODE_MULTIPLE
. In that case, I don't have to deal with each item, but I can retrieve an array of the items selected by using listview.getCheckedItemPositions()
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater li = LayoutInflater.from(getActivity());
View view= li.inflate(R.layout.listview_dialog, null);
builder.setView(view);
builder.setTitle(title);
listView = (ListView) view.findViewById(R.id.listview);
ArrayAdapter<String> ad = new ArrayAdapter<String>(getActivity(), R.layout.dialog_list_item , R.id.text2, tables);
listView.setAdapter(ad);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setDivider(null);
builder.create();