CursorAdapter - 列表行为
CursorAdapter - List beheivour
我使用 CursorAdapter 为我的列表设置一个单独的自定义布局,这取决于我的 table 的字段值(true 或 false),列表项中的文本视图之一的颜色将与众不同。
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" 为 false 时保持红色?
谢谢
你需要记住以下几点
1、ListView中的views是有限的。
2. 每当你滚动可见性消失的视图时,它会更新为后面的项目,然后它会回到视图
所以现在您要根据一些标准设置颜色。每当您绑定视图时,您都会设置颜色。
确保在取消绑定视图时,您将使视图恢复到原始颜色。如果你不这样做,当失去可见性时,视图仍然包含颜色,当其他数据项被提取到它时,它看起来是红色的。
所以绑定的时候先看看是不是红色。如果是,则检查是否要保持红色,是保持红色,否,然后使其恢复原始颜色
我使用 CursorAdapter 为我的列表设置一个单独的自定义布局,这取决于我的 table 的字段值(true 或 false),列表项中的文本视图之一的颜色将与众不同。
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" 为 false 时保持红色?
谢谢
你需要记住以下几点 1、ListView中的views是有限的。 2. 每当你滚动可见性消失的视图时,它会更新为后面的项目,然后它会回到视图
所以现在您要根据一些标准设置颜色。每当您绑定视图时,您都会设置颜色。 确保在取消绑定视图时,您将使视图恢复到原始颜色。如果你不这样做,当失去可见性时,视图仍然包含颜色,当其他数据项被提取到它时,它看起来是红色的。
所以绑定的时候先看看是不是红色。如果是,则检查是否要保持红色,是保持红色,否,然后使其恢复原始颜色