Ive a listview with a MultiChoiceModeListener and a ChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL). This is working as expectet. When I slect an item, the item is highlighted, even when I choose multiple ones.
I have a customArrayAdapter within I set alternate row colors in the listview. And here the problem lies: At time when I start using the alternate row colors in the customArrayAdapter, the highlighting when selecting an item isnt working anymore!
It seemes that the highlighting happens in background of my alternate row colors.
setMultiChoiceModeListener
private void initializeContextualActionBar() {
final ListView shoppingMemosListView = (ListView) findViewById(R.id.listview_shopping_memos);
shoppingMemosListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
shoppingMemosListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
int selCount = 0;
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (checked) {
selCount++;
} else {
selCount--;
}
String cabTitle = selCount + " " + getString(R.string.cab_checked_string);
mode.setTitle(cabTitle);
mode.invalidate();
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
getMenuInflater().inflate(R.menu.menu_contextual_action_bar, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
MenuItem item = menu.findItem(R.id.cab_change);
if (selCount == 1) {
item.setVisible(true);
} else {
item.setVisible(false);
}
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
boolean returnValue = true;
SparseBooleanArray touchedShoppingMemosPositions = shoppingMemosListView.getCheckedItemPositions();
switch (item.getItemId()) {
case R.id.cab_delete:
for (int i = 0; i < touchedShoppingMemosPositions.size(); i++) {
boolean isChecked = touchedShoppingMemosPositions.valueAt(i);
if (isChecked) {
int postitionInListView = touchedShoppingMemosPositions.keyAt(i);
ShoppingMemo shoppingMemo = (ShoppingMemo) shoppingMemosListView.getItemAtPosition(postitionInListView);
dataSource.setDeleteShoppingMemo(shoppingMemo);
}
}
showAllListEntries();
mode.finish();
break;
case R.id.cab_change:
for (int i = 0; i < touchedShoppingMemosPositions.size(); i++) {
boolean isChecked = touchedShoppingMemosPositions.valueAt(i);
if (isChecked) {
int postitionInListView = touchedShoppingMemosPositions.keyAt(i);
ShoppingMemo shoppingMemo = (ShoppingMemo) shoppingMemosListView.getItemAtPosition(postitionInListView);
AlertDialog editShoppingMemoDialog = createEditShoppingMemoDialog(shoppingMemo);
editShoppingMemoDialog.show();
Button theButton = editShoppingMemoDialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListenerEditShoppingMemoDialogPositiv(editShoppingMemoDialog, shoppingMemo));
}
}
mode.finish();
break;
default:
returnValue = false;
break;
}
return returnValue;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
selCount = 0;
}
});
}
customArrayAdapter
public class ShoppingMemoArrayAdapter extends ArrayAdapter<ShoppingMemo> {
public static final String LOG_TAG = ShoppingMemoArrayAdapter.class.getSimpleName();
private Context mContext;
private List<ShoppingMemo> mShoppingMemo;
private LayoutInflater mLayoutInflater;
public ShoppingMemoArrayAdapter(Context context, List<ShoppingMemo> shoppingMemoList) {
super(context, R.layout.list_row, shoppingMemoList);
mContext = context;
mShoppingMemo = shoppingMemoList;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences sharedPrefs = mContext.getSharedPreferences("de.tobias.shoppinglist_preferences", mContext.MODE_PRIVATE);
Boolean prefShowCount = sharedPrefs.getBoolean("show_count", false);
ShoppingMemo memo = mShoppingMemo.get(position);
View rowView;
if (convertView == null) {
rowView = mLayoutInflater.inflate(R.layout.list_row, parent, false);
}
else {
rowView = convertView;
}
TextView tvProduct = (TextView) rowView.findViewById(R.id.listview_row_product);
TextView tvCount = (TextView) rowView.findViewById(R.id.listview_row_count);
CheckBox tvChecked = (CheckBox) rowView.findViewById(R.id.listview_row_checked);
tvChecked.setChecked(memo.isChecked());
tvProduct.setText(memo.getProduct());
if(memo.getQuantity() > 1 || prefShowCount)
{
tvCount.setText(Integer.toString(memo.getQuantity()));
}else{
tvCount.setText("");
}
if (memo.isChecked()) {
tvProduct.setPaintFlags(tvProduct.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
tvProduct.setTextColor(Color.rgb(175,175,175));
tvCount.setTextColor(Color.rgb(175,175,175));
rowView.setBackgroundColor(Color.BLACK);
}
else {
tvProduct.setPaintFlags( tvProduct.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
tvProduct.setTextColor(Color.DKGRAY);
tvCount.setTextColor(Color.DKGRAY);
if (position % 2 == 1) {
rowView.setBackgroundColor(Color.LTGRAY);
} else {
rowView.setBackgroundColor(Color.WHITE);
}
}
return rowView;
}
}
expectet expectet not working not working
I tried setting the highlighting whithin the customArrayAdapter, but the seletcted state was not probebly there.
I tried setting the highlighting whithin the setMultiChoiceModeListener but that was alwayse very weired und overwritten from the customArrayAdapter.
Does anyone has an idear what to do here?