I\'m trying to change the row color of my listView in customAdapter. There\'s an array of integer that include 0 and 1, I\'m trying to read from the array and change the color o
getView()
is called for each row in a list so you should not loop over all items in test
but only handle the one denoted by position
and you should do that after (not in) the if (convertView == null)
block.
Edit:
getView()
should do the following things:
Now we have a valid View for the list row.
test.get(position)
. The position
is the number of the requested row (starting with 0 at the top of the ListView).position
).In more complex situations you may have to do the third step before the second but not here.
Fast solution (not very nice code, but works):
@Override
public int getItem(int position){
return test.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int color0 = ....
int color1 = ....
int colorDefault = ...
switch (test.get(position)) {
case 0:
convretview.setBackgroundColor(color0);
break;
case 1:
convretview.setBackgroundColor(color1);
break;
default:
convretview.setBackgroundColor(colorDefault);
}
...
}