【发布时间】:2016-09-16 09:58:39
【问题描述】:
我为我的列表使用了一个带有一个自定义布局的 CursorAdapter,根据我的表格字段的值(真或假),列表项中的一个文本视图的颜色会有所不同。
public class CustomAdapter extends CursorAdapter {
DatabaseHelper myDB;
SQLiteDatabase db;
public CustomAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, 0);
myDB = new DatabaseHelper(context, DatabaseHelper.DB_NAME, null, DatabaseHelper.DB_VERSION_SCHEME);
db = myDB.getReadableDatabase();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.customize_cell_list, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tv = (TextView) view.findViewById(R.id.tv);
TextView tv_date = (TextView) view.findViewById(R.id.tvDateCursor);
TextView tv_time = (TextView) view.findViewById(R.id.tvTime);
ImageView img = (ImageView)view.findViewById(R.id.img);
if(cursor != null) {
String tv = cursor.getString(2);
String date = cursor.getString(3);
String time = cursor.getString(8);
boolean active = cursor.getInt(12) > 0;
boolean go = cursor.getInt(13) > 0;
tv_date.setText(date);
tv.setText(tv);
if(go) { // OPTION 1
img.setImageResource(R.drawable.calle);
tv_time.setText(time);
} else { // OPTION 2
img.setImageResource(R.drawable.casa);
if(active){ // OPTION 2.1
tv_time.setText("Activado");
}else{ // OPTION 2.2
tv_time.setText("No activado");
tv_time.setTextColor(Color.RED);
}
}
}
}
}
结果:图像显示与文本视图相同的正确图像,问题是颜色,当我有一个带有 OPTION 2.2 的项目并开始上下滚动我的一些 tv_time(甚至从选项 1 开始)时将颜色更改为红色,然后我再次滚动,它们变为白色,其他变为红色,这是随机的。
为什么会这样?只有当“go”和“active”为假时,如何才能保持红色?
谢谢
【问题讨论】:
-
谢谢,如果你能指出一个类似的问题,我会删除我的问题。
-
始终使用 ViewHolder。 findViewById() 是一项昂贵的任务,会在滚动时带来闪烁效果
标签: android android-cursoradapter