Reputation: 1234
Here is my problem: I have a ListView with a custom ArrayAdapter:
My list in onCreate()
:
list = /*some list*/
listView = (ListView) findViewById(R.id.some_list);
listView.setAdapter(new MapListAdapter(this, R.layout.custom_list_item, list));
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listView.setItemChecked(position, true);
}
});
mapListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public boolean onPrepareActionMode(...) {
// some unrelated working code
}
@Override
public void onDestroyActionMode(...) {
// some unrelated working code
}
@Override
public boolean onCreateActionMode(...) {
// some unrelated working code
}
@Override
public boolean onActionItemClicked(...) {
// some unrelated working code
}
@Override
public void onItemCheckedStateChanged(...) {
// some unrelated working code
}
});
My custom ArrayAdapter's getView(...)
:
@Override
public View getView (int position, View view, ViewGroup parent) {
view = (LinearLayout) inflater.inflate(layoutId, null);
final ListItem entry = getItem(position);
// setting some textviews here and so on...
CheckBox checkBox = (CheckBox) view.findViewById(R.id.m_l_i_checkbox);
// my problem ^
return view;
}
I know that certain list items are selected, because ActionMode bar appears as I select them and disappears as I unselect(toggle) all of them. However, I want to have checkBox that would indicate if they indeed are selected. So how do I do that? It works just fine with android.R.single_list_item_multiple_choice and default ArrayAdapter, but I want to use my custom one. Maybe I'm getting something wrong and there is another way to do it?
Upvotes: 0
Views: 5472
Reputation: 3137
I implement a similar behavior custom adapter by adding a "special" field in my Item class called isSelectedOnList (Boolean). Then in the OnItemClickListener I do like this:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mapListView.setItemChecked(position, true);
...Get your item here...getItem(position).setIsSelectedOnList(!getItem(position).isSelectedOnList());
}
});
So now my Items Array knows which items are selected. Then in the getView method I update the checkbox which is in my item layout xml:
@Override
public View getView (int position, View view, ViewGroup parent) {
view = (LinearLayout) inflater.inflate(layoutId, null);
final ListItem entry = getItem(position);
// setting some textviews here and so on...
CheckBox checkBox = (CheckBox) view.findViewById(R.id.m_l_i_checkbox);
// my problem ^
checkBox.setChecked(ListItem.isSelectedOnList());
return view;
}
This solution works for me pretty well so I didn't care to look in android's internal mechanism for selected items in multi-select lists.
Hope this helps...
Upvotes: 2