【问题标题】:Listview item value is changed on scrolling in custom cursor adapter在自定义光标适配器中滚动时更改 Listview 项目值
【发布时间】:2015-12-22 14:53:46
【问题描述】:

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

@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);


            }
        }
    });
}

【问题讨论】:

  • 感谢您的回复。我知道列表视图回收,我知道如何控制简单适配器(基于模型)中的值更改,但我面临光标适配器的问题。
  • 我认为问题在于,当 ListView 滚动时,bindView 被再次调用,当它被调用时,就好像它还没有被点击一样,所以图像恢复为默认值。您需要找到一种方法来标记该项目以知道要使用哪个图像。
  • 这是我的问题,如何停止这种变化。在简单的适配器中(使用模型和 getview,我们可以在项目变化时更改模型,因此这种变化仍然存在)但是如何使用光标来完成。

标签: android listview android-listview cursor android-cursoradapter


【解决方案1】:

随着列表进一步向上/向下滚动,指向您正在查看的填充数据的位置的引用会发生变化。换句话说,当项目离开屏幕并需要重新呈现在屏幕上时,该列表位置已经被处理并从中移动,因此数据将不正确。

我会尝试实现自定义 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;
}

}

编辑:

哦,请确保您正在实施一种方法来回收您的位图 onPause,否则您将遇到 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();

每次你想改变一个值

【讨论】:

  • 我正面临光标适配器的问题。我已经可以使用 baseadapter 完成了。如果您可以帮助使用光标适配器,那将是很大的帮助。谢谢
  • 是否可以使用收藏项的封装较少(即更易于访问)的版本?就像在光标结果中检查 isFavorite 一样,甚至存储收藏的 Id 的数组列表?
  • 我猜维护一个单独的数组列表将是额外的工作量
  • 我的光标中有 ivFavourite 检查
  • 问题是当我做任何喜欢的项目时,我如何告诉我的光标让它成为最爱
猜你喜欢
  • 1970-01-01
  • 2020-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多