This problem is not new. However, any of the solution is not working for me.
I have got a ListView with several TextView which is populated from data fetched from SQLite. User can LongPress on one item to select multiple items of ListView and further choose to delete them from database.
What I want to do next is to highlight those items which are selected by user after the first LongPress (including the first one).
Here is the snippet of my ListView.
<ListView
...
android:drawSelectorOnTop="false"
android:listSelector="@android:color/darker_gray"
android:choiceMode="multipleChoice"/>
This is highlighting the item of ListView but not the first one on which the user will do the LongPress. And only highlight one of the item on ListView, whenever user chooses another item the current one is resetted and the background of the new item is changed, and this continues.
EDIT:
Here is my JAVA implementation of setMultiChoiceModeListener
list.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
final int checkedCount = list.getCheckedItemCount();
mode.setTitle(checkedCount + " Selected");
listViewAdapter.toggleSelection(position);
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.hold_activity_actions, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
SparseBooleanArray selected = listViewAdapter.getSelectedItemIds();
switch (item.getItemId()) {
case R.id.delete:
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
bloodPressureRecords selectedItem = listViewAdapter.getItem(selected.keyAt(i));
// Remove selected items following the ids
listViewAdapter.remove(selectedItem);
db.deleteRecord(selectedItem);
}
}
mode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
listViewAdapter.removeSelection();
}
});