简体   繁体   中英

ListView item background color

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 ?

add an else statement to the color change

    if (testList.get(position).progress == 0){
        rowView.setBackgroundColor(Color.parseColor("#BDBDBD"));
    } else {
        rowView.setBackgroundColor(Color.parseColor("#Color of the other cells"));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM