I'm currently using a ListView to display a sort of items. I've implemented an action mode to select multiple items and to delete massively that works well in android 4.x. But when I tried with API version 8 or 9 (android 2.2.x/2.3.x), selection works internally as expected but row items are colored randomly.
If user selects first row, internally first row is selected, but row number 4 is colored. When I click another row, this row and first one is colored. It's a strange behaviour which I expect to work normally as on 4.x devices.
Long-click overriding to activate action mode and check long-clicked item of ListView:
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
if (actionMode == null) {
listView.setOnItemClickListener(new CABClickListener());
actionMode = startActionMode(new ListActionMode());
// Check item pressed with long click
listView.setItemChecked(position, true);
view.setBackgroundColor(checkedColor);
logger.debug("Item at pos. " + position + ", checked.");
}
return true;
}
CABClickListener, responsible to check/uncheck items of ListView, marking them internally and changing its background color:
private final class CABClickListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (listView.isItemChecked(position)) {
view.setBackgroundColor(checkedColor);
logger.debug("Item at pos. " + position + ", checked.");
} else {
view.setBackgroundColor(uncheckedColor);
logger.debug("Item at pos. " + position + ", unchecked.");
}
}
}
Those classes/methods are inside the Activity, listView
is declared in top of it.
More considerations:
- Using ActionBarSherlock (it shows the CAB but I think this is not important here) and Roboguice, but I haven't any problem with that.
I was always developing with the emulator. In addition, I couldn't try my app with android 3.x (got problems with this version, emulator doesn't launch), so I don't know if the problem persists in these versions.UPDATE: Tested in android 3.0 API 11, works well as on 4.x.- I debugged the code and
View
s in both methods are ok, but when I callview.setBackgroundColor(checkedColor);
, anotherView
is colored.
Any suggestion? Hope anyone can help!