I\'m working on a ListActivity which will display a bunch of numbers (weights). I would like to change the background of a specific row in the ListView. To do this I have cr
I did a bit of research to find out how this should be done properly.
I'm writing this down for the others with the same problem, as I guess this is the proper way how to do it. Please, let me know, if I am mistaken or if this solution has any flaws I'm not seeing.
public class WeightListAdapter extends ArrayAdapter<Integer> {
private static final int TYPE_COUNT = 2;
private static final int TYPE_ITEM_COLORED = 1;
private static final int TYPE_ITEM_NORMAL = 0;
public WeightListAdapter(Context context, List<Integer> objects) {
super(context, android.R.layout.simple_list_item_1, objects);
}
@Override
public int getViewTypeCount() {
return TYPE_COUNT;
}
@Override
public int getItemViewType(int position) {
int item = getItem(position);
return (item == 30) ? TYPE_ITEM_COLORED : TYPE_ITEM_NORMAL;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
switch (getItemViewType(position)) {
case TYPE_ITEM_COLORED:
v.setBackgroundColor(Color.YELLOW);
break;
case TYPE_ITEM_NORMAL:
break;
}
return v;
}
}
Apparently the base class already implements the logic ensuring the correct convertView
is passed to the getView
method (based on the getViewItemType
and getViewTypeCount
methods).
If it's due to reuse, why don't you add another check for if itemWeight not equal to 20? If not equal, then set the background back to normal.
Android's view is re-use a component for each row. I got this problem too.