0

我有一个通过自定义光标适配器生成的列表视图

一切正常,除了当我在列表中上下滚动时,一些文本视图随机变为绿色。

这是我生成列表视图的代码:

private class AchievementAdapter extends CursorAdapter {
    public AchievementAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public void bindView(View v, Context context, Cursor cursor) {
        TextView tv = (TextView) v.findViewById(R.id.achView1);

        if(cursor.getString(cursor.getColumnIndex("completed")).equals("yes")) {
            tv.setText(cursor.getString(cursor.getColumnIndex("name"))+" (completed)");
            tv.setTextColor(Color.GREEN);
        }
        else {
            tv.setText(cursor.getString(cursor.getColumnIndex("name")));
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.achievements_item, parent, false);
        return v;
    }
}    

我读了一些关于 setcachecolorhint 的内容,但该方法不适用于 textview。我该如何解决这个问题,以便在我滚动时停止随机更改 textview 颜色?

4

1 回答 1

6

您应该在else语句中将 textView 颜色设置为正常,例如:

@Override
public void bindView(View v, Context context, Cursor cursor) {
    TextView tv = (TextView) v.findViewById(R.id.achView1);

    if(cursor.getString(cursor.getColumnIndex("completed")).equals("yes")) {
        tv.setText(cursor.getString(cursor.getColumnIndex("name"))+" (completed)");
        tv.setTextColor(Color.GREEN);
    }
    else {
        tv.setText(cursor.getString(cursor.getColumnIndex("name")));
        tv.setTextColor(Color.BLACK); // Set your textview color as you need
    }
}

希望这可以帮助

于 2012-05-31T18:04:31.143 回答