如何防止列表视图缓存

How to prevent listview caching

上下文:

我有一个 activity 列出了游标返回的所有消息线程,我将它们绑定到 ListView。

我的目标是:

我想根据游标的任意值自定义ListView的任意行。我的意思是如果线程包含未读消息,我想将文本颜色设置为蓝色 (=> "not read").

问题是:

Listview 在滚动时确实缓存了一些视图,因此一些读取了所有消息的线程随机显示为蓝色。

我尝试了所有这些选项,但它一直在缓存:

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

有什么建议吗?


编辑 1

我试图在扩展 SimpleCursorAdapter:

的 class 中以这种方式覆盖 bindView
@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);

    }

}

尝试

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

由于listView中使用的视图是回收的,所以你需要处理所有的情况。