-1

I am using a listview with cursor adapter.This listview has a imageview in each item when i click on this image view i change the imageview image which is changing properly but when i scroll my list imageview changes to its orignal view. Note : i am using cursor adapter.(I know about listview recycling and i know to to control value change in simple adapter(model based)) Here is my cursor adapter :

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


            }
        }
    });
}
Hussnain Azam
  • 358
  • 1
  • 5
  • 14
  • Possible duplicate of [How ListView's recycling mechanism works](http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works) – 2Dee Dec 22 '15 at 14:56
  • thanks for reply.I know about listview recycling and i know how to control value changing in simple adapter(Model based) but i am facing issues with cursor adapter. – Hussnain Azam Dec 22 '15 at 15:01
  • I think the problem is because as the ListView scrolls bindView is called again and when it's recalled it's as if it hasn't been clicked yet so the image goes back to default. You need to find a way to flag that item to know which image to use. – AdamMc331 Dec 22 '15 at 15:04
  • thats my question how to stop that change.In simple adapters (with model and getview we can change model on item change so this change persists) but how it can be done with a cursor. – Hussnain Azam Dec 22 '15 at 15:07

1 Answers1

0

As the list scrolls further up/down, the reference held to the position you are looking at which populate the data changes. In other words, when the items are offscreen and need to be rendered back on screen, that list position has already been processed and moved on from so the data will be incorrect.

I would try implementing a custom BaseAdapter and inflate your controls. If you give me a moment I will even do that for you...

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;
}

}

EDIT:

Oh and ensure you are implementing a method to recycle your bitmaps onPause or you will wind up with OOM exceptions

Or if you are still going to use the CursorAdapter add this method to your SQLiteDatabase:

in your database add the method

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)}]);

}

and then call: cursor.requery(); notifyDataSetChanged();

Everytime you want to change a value

KoalaKoalified
  • 687
  • 4
  • 15