简体   繁体   中英

How to prevent listview caching

Context:

I have an activity that lists all message threads returned by a cursor and I bind them to a ListView.

What I aim at:

I would like to customize any row of the ListView according to any value of the cursor. I mean if the thread contains not read messages, I want to set the textcolor to blue (=> "not read").

The problem is:

The Listview does cache some views while scrolling so some threads with all messages read appear randomly in blue.

I tried all of these option but it kept caching:

view.setDrawingCacheEnabled(false);
view.setAnimationCacheEnabled(false);
view.setScrollingCacheEnabled(false);
view.setAlwaysDrawnWithCacheEnabled(false);
view.setDrawingCacheEnabled(false);
view.setWillNotCacheDrawing(true);
view.invalidate();

Any advice?


EDIT 1

I tried to override bindView this way in a class that extends SimpleCursorAdapter:

@Override
public void bindView(View v, Context context, Cursor c) {

    TextView tv_name = (TextView) v.findViewById(R.id.tv_name);

    //we set the name of the src/dst of the msg
    tv_name.setText(c.getString(5));

    //if there are not read msg, we set the textColor to BLUE
    if (cursor.getInt(8) != 0){

            tv_name.setTextColor(Color.BLUE);

    }

}

Try

if (cursor.getInt(8) != 0){
        tv_name.setTextColor(Color.BLUE);
} else{
        // set text to the default color
        tv_name.setTextColor(Color.BLACK);       
}

Since the view used in listView is recycled, you need to handle all the cases.

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