我正在使用带有光标适配器的列表视图。当我点击此图像视图时,此列表视图在每个项目中都有一个图像视图我更改了正确更改的图像视图图像,但是当我滚动列表时,图像视图更改为其原始视图。 注意:我正在使用游标适配器。(我知道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);
}
}
});
}
答案 0 :(得分:0)
当列表进一步向上/向下滚动时,保持在您正在查看填充数据的位置的引用会发生变化。换句话说,当项目在屏幕外并需要在屏幕上呈现时,该列表位置已经被处理并继续运行,因此数据将不正确。
我会尝试实现自定义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();
每次您想要更改值