Reputation: 1734
I've got an odd problem. Everything worked fine a couple of iterations ago, but now it's not working anymore. I've set up a setMultiChoiceModeListener to let the user choose multiple elements to delete from a ListView. To mark elements as checked I did this:
public void onItemCheckedStateChanged(ActionMode mode,int position, long id, boolean checked){
Log.d(TAG,(checked ? "Checked " : "Un-checked ")+ ((TextView) listView.getChildAt(position).findViewById(R.id.label)).getText().toString());
if (checked){
listView.getChildAt(position).setBackgroundColor(Color.parseColor("#6633b5e5"));
}else{
listView.getChildAt(position).setBackgroundColor(Color.parseColor("#00000000"));
}
}
The Log statement is to debug, and it outputs correctly. On the first long press which activates the MultiChoiceModeListener it outputs "Checked text". I've also checked the color of it with a Log statement and it outputs the right color value. Only problem is, it's not drawn. If I keep selecting items they will change color, and if I deselect and re-select the item it changes color. So it's only the first checked item that doesn't change color, even if it's counted as checked in for example onActionItemClicked. This entire thing seem a bit tedious so I'm also wondering if there is a better way to do this. I've looked into setting up selectors in html files but without luck. I'm able to change the pressed color but not the checked color.
Any help appreciated.
Upvotes: 1
Views: 2512
Reputation: 2946
You are trying to use a resource so you should be doing the following
listView.getChildAt(position).setBackgroundResource(Color.parseColor(<Your-Color>))
Also if you want to skip that parsing, you can just put that color in your color.xml
and reference it using a R.color.<your-color-name>
instead.
listView.getChildAt(position).setBackgroundResource(R.color.<Your-Color>)
Upvotes: 2