Android 在自定义游标适配器中滚动时更改Listview项值

Android 在自定义游标适配器中滚动时更改Listview项值,android,listview,android-listview,cursor,android-cursoradapter,Android,Listview,Android Listview,Cursor,Android Cursoradapter,我正在使用带光标适配器的listview。当我单击此图像视图时,此listview在每个项目中都有一个imageview。我更改了正在正确更改的imageview图像,但当我滚动列表时,imageview更改为其原始视图。 注意:我使用的是游标适配器(我知道listview循环,我知道如何控制简单适配器中的值更改(基于模型)) 这是我的游标适配器: @Override public void bindView(View view, final Context context, Cursor cu

我正在使用带光标适配器的listview。当我单击此图像视图时,此listview在每个项目中都有一个imageview。我更改了正在正确更改的imageview图像,但当我滚动列表时,imageview更改为其原始视图。 注意:我使用的是游标适配器(我知道listview循环,我知道如何控制简单适配器中的值更改(基于模型)) 这是我的游标适配器:

@Override
public void bindView(View view, final Context context, Cursor cursor) {
    spotsImage = (SimpleDraweeView) view.findViewById(R.id.spotsImage);
    ivFavourite = (ImageView) view.findViewById(R.id.favouriteButton);
        ivFavourite.setTag(cursor.getString(cursor.getColumnIndex(Constants.PEEP_ID))+"tag"+cursor.getInt(cursor.getColumnIndex(Constants.PEEP_STATUS)));
    spotsTitle = (TextView) view.findViewById(R.id.titleTextView);
    followerscount = (TextView) view.findViewById(R.id.distanceTextView);

    spotsTitle.setText(cursor.getString(cursor.getColumnIndex(Constants.PEEP_NAME)));


    Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Constants.PEEP_PROFILE)));
    spotsImage.setImageURI(uri);

     count = cursor.getInt(cursor.getColumnIndex(Constants.PEEP_FOLLOWER_COUNT));
    if (count > 1){
        followerscount.setVisibility(View.VISIBLE);
        followerscount.setText(count+" followers");
    }
    else if (count == 1){
        followerscount.setVisibility(View.VISIBLE);
        followerscount.setText(count+" follower");
    }
    else {
        followerscount.setVisibility(View.INVISIBLE);
    }

    if (cursor.getInt(cursor.getColumnIndex(Constants.PEEP_STATUS)) == 1) {
        ivFavourite
                .setImageResource(R.drawable.favourites_tapped);
    } else {
        ivFavourite.setImageResource(R.drawable.favourites);
    }

    ivFavourite.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){

            String [] Tags = ((String) v.getTag()).split("tag");

            if (Tags[1].equals("1")) {



                    ((ImageView) v).setImageResource(R.drawable.favourites);

            }
            else {

                    ((ImageView) v).setImageResource(R.drawable.favourites_tapped);


            }
        }
    });
}

当列表进一步向上/向下滚动时,保存到您正在查看的填充数据的位置的引用将发生更改。换句话说,当项目在屏幕外并且需要重新呈现在屏幕上时,该列表位置已被处理并从中移动,因此数据将不正确

我会尝试实现一个定制的BaseAdapter并为您的控件充气。如果你给我一点时间,我甚至会为你这样做

public class MyAdapter extends BaseAdapter {
    private Cursor cursor = null;
    private Context context = null;
    public MyAdapter(Context context, Cursor cursor)
    {
        this.context = context;
        if (cursor != null)
        {
            cursor.moveToFirst();
            this.cursor = cursor;
        }
    }
@Override
public int getCount() {
    return cursor.getCount();
}

@Override
public Object getItem(int position) {
    //processed at runtime
    return null;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    cursor.moveToPosition(position);
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.mylistitem);
    spotsImage = (SimpleDraweeView) view.findViewById(R.id.spotsImage);
    ivFavourite = (ImageView) view.findViewById(R.id.favouriteButton);
    ivFavourite.setTag(cursor.getString(cursor.getColumnIndex(Constants.PEEP_ID))+"tag"+cursor.getInt(cursor.getColumnIndex(Constants.PEEP_STATUS)));
    spotsTitle = (TextView) view.findViewById(R.id.titleTextView);
    followerscount = (TextView) view.findViewById(R.id.distanceTextView);
    Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Constants.PEEP_PROFILE)));
    spotsImage.setImageURI(uri);
    count = cursor.getInt(cursor.getColumnIndex(Constants.PEEP_FOLLOWER_COUNT));
    if (count > 1){
        followerscount.setVisibility(View.VISIBLE);
        followerscount.setText(count+" followers");
    }
    else if (count == 1){
        followerscount.setVisibility(View.VISIBLE);
        followerscount.setText(count+" follower");
    }
    else {
        followerscount.setVisibility(View.INVISIBLE);
    }

    if (cursor.getInt(cursor.getColumnIndex(Constants.PEEP_STATUS)) == 1) {
        ivFavourite
                .setImageResource(R.drawable.favourites_tapped);
    } else {
        ivFavourite.setImageResource(R.drawable.favourites);
    }

    ivFavourite.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            String [] Tags = ((String) v.getTag()).split("tag");
            if (Tags[1].equals("1")) {
                ((ImageView) v).setImageResource(R.drawable.favourites);
            }
            else {
                ((ImageView) v).setImageResource(R.drawable.favourites_tapped);
            }
        }
    });
    return convertView;
}
}

编辑:

哦,请确保您正在实施一种在暂停时回收位图的方法,否则您将遇到OOM异常

或者,如果仍要使用CursorAdapter,请将此方法添加到SQLiteDatabase:

在数据库中添加方法

public void updatePeepStatusById(int id, int newStatus)
{
    SQLiteDatabase db = getWriteableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(Constants.PEEP_STATUS, newStatus);
    db.update(PEEPTABLE, cv, PEEPID + " = ?", new String[{Integer.toString(id)}]);

}
然后打电话: cursor.requery(); notifyDataSetChanged()


每次您想要更改值时

可能会重复感谢回复。我知道listview回收,并且知道如何在简单适配器(基于模型)中控制值更改但是我面临着游标适配器的问题。我认为问题在于,当ListView滚动bindView时,它被再次调用,当它被调用时,它就好像还没有被单击过一样,因此图像返回到默认值。您需要找到一种方法来标记该项目,以知道要使用哪个图像。这是我的问题,如何停止该更改。在简单适配器中(使用model和getview,我们可以在项目更改时更改模型,以便此更改持续),但如何使用光标完成。我面临光标适配器的问题。我已经可以用baseadapter完成了。如果你能用游标适配器提供帮助,那将是一个很大的帮助。Thank是否可以使用受欢迎项的封装较少的版本(即更容易访问)?正如在游标结果中使用isFavorite检查一样,或者甚至存储一个favorite ID的数组列表?维护一个单独的值数组列表将是额外的工作量,我想我在游标中使用了Ivorite检查。问题是,当我做任何项目favorite时,我如何告诉我的游标将其设置为favorite