简体   繁体   English

SimpleCursorAdapter在滚动过程中弄乱了listview项目

[英]SimpleCursorAdapter messes up listview items during scrolling

I have a listview which displays food deliveries and its items are set by a custom SimpleCursorAdapter. 我有一个显示食物交付的列表视图,其项目由自定义的SimpleCursorAdapter设置。

I am displaying an extra icon for each food delivery or listview item that includes special offers etc. I am also sorting the list so the food deliveries with the special offers to be on top. 我为每个包括外卖商品在内的食品交付或列表视图项目显示一个额外的图标。我也在对列表进行排序,以便将外卖商品与特价商品放在首位。

When the listview is first initialized, everything is good. 首次初始化listview时,一切都很好。 When you scroll then items that do not include special offer have also the icon and the listview is messed up. 当您滚动时,不包括特惠的项目也带有图标,并且列表视图被弄乱了。

I guess the problem is coming from the view recycling but I do not know how to solve it. 我猜问题出在视图回收中,但我不知道如何解决。

Here is my SimpleCursorAdapter class 这是我的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){
    // 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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM