【问题标题】:SimpleCursorAdapter messes up listview items during scrollingSimpleCursorAdapter 在滚动期间弄乱了列表视图项
【发布时间】:2017-10-10 11:29:56
【问题描述】:

我有一个显示送餐的列表视图,其项目由自定义 SimpleCursorAdapter 设置。

我为每个送餐或包含特别优惠等的列表视图项目显示一个额外的图标。我还在对列表进行排序,以便将带有特别优惠的送餐放在首位。

当列表视图第一次初始化时,一切都很好。当您滚动时,不包括特价商品的项目也有图标,并且列表视图混乱。

我猜这个问题来自视图回收,但我不知道如何解决它。

这是我的 SimpleCursorAdapter 类

private class MyCursorAdapter extends SimpleCursorAdapter{
    private int rowLayout;
    private Cursor cursor;

    public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.rowLayout=layout;
        this.cursor=c;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        return inflater.inflate(this.rowLayout,null);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        // get resources from database
        int id = cursor.getInt(0);
        int title = cursor.getInt(1);
        final int specialOffer = cursor.getInt(7);
        if (specialOffer==1){
            // we have a special offer delivery
            view.findViewById(R.id.offer_imageview).setVisibility(View.VISIBLE);
        }
    }
}

【问题讨论】:

  • 如果条件不成立,您需要在if (specialOffer==1) 上添加一个else 来隐藏ImageView
  • 好吧,那太尴尬了……非常感谢!

标签: android listview simplecursoradapter


【解决方案1】:
 if (specialOffer==1){
    // we have a special offer delivery
     view.findViewById(R.id.offer_imageview).setVisibility(View.VISIBLE);
 }
 else{
     view.findViewById(R.id.offer_imageview).setVisibility(View.GONE);
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-28
    • 2017-03-01
    • 2017-02-11
    • 2010-11-06
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多