【问题标题】:CursorAdapter - List beheivourCursorAdapter - 列出行为
【发布时间】: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


【解决方案1】:

你需要记住以下几点 1. ListView 中的视图是有限的。 2. 每当您滚动可见性消失的视图时,都会使用后面的项目进行更新,然后它会回到视图

所以现在您正在根据一些标准设置颜色。并且每当您绑定视图时,您都在设置颜色。 确保取消绑定视图时,您将使视图恢复到原始颜色。如果您不这样做,则视图在失去可见性时仍然包含颜色,并且当其他数据项被提取到其中时,它看起来是红色的。

所以在绑定的时候先检查是否是红色的。如果是,那么检查你想保持红色,是的保持它,然后让它回到原来的颜色

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多