Android : ListView item background color

on Sunday, September 21, 2014


Today I have faced a strange problem in implementing an adapter for ListView.



@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;

if (convertView == null){
LayoutInflater inflater = (LayoutInflater) appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = (View) inflater.inflate(R.layout.test_list_item, parent , false);


}else{
rowView = (ViewGroup) convertView;
}




Log.d(String.valueOf(position), String.valueOf(testList.get(position).progress));
if (testList.get(position).progress == 0){
rowView.setBackgroundColor(Color.parseColor("#BDBDBD"));
}
return rowView;
}


And here is the LogCat output.



tag msg
D/0 : 100
D/1 : 90
D/2 : 80
D/3 : 70
D/4 : 0
D/5 : 0
D/6 : 0
D/0 : 100
D/1 : 90
D/2 : 80
D/3 : 70
D/4 : 0
D/5 : 0
D/6 : 0


As you can see for all indexes besides 0 , 1 , 2 , 3 the background color should change. But when the the activity starts all the rows change their background color besides 2-nd , 3-rd , 4-th rows (the first one changes too though it's progress is grater than 0). Moreover when I scroll all the way down and up again all the rows appear to change their background color including rows number 2 ,3 ,4. How can I make the rows only with progress equal to 0 change their color ?


0 comments:

Post a Comment