I know it sounds very simple, and there are questions about this. But none of it could solve my problem. So here we go:
I want to change background color of a list item
Sure thing. I would do this in the getView()
method of a custom ListAdapter
.
MyAdapter extends SimpleAdapter {
private ArrayList coloredItems = new ArrayList();
public MyAdapter(...) {
super(...);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (coloredItems.contains(position)) {
v.setBackgroundColor(Color.CYAN);
} else {
v.setBackgroundColor(Color.BLACK); //or whatever was original
}
return v;
}
}
Update coloredItems
when a list item is clicked.
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
if (coloredItems.contains(position)) {
//remove position from coloredItems
v.setBackgroundColor(Color.BLACK); //or whatever was original
} else {
//add position to coloredItems
v.setBackgroundColor(Color.CYAN);
}
}