Reputation: 437
I have a list view and attach a custom CursorAdapter
to it. Each item in list view have an image that i want to change this image when onClick
occurred.
For example, when I click on image with position 4, image of position 10 changed and 4 not changed. and etc.
Why this happen?
My custom CursorAdapter
is:
public class CustomFehrestAdapter extends CursorAdapter {
String Tag;
ImageView favic;
SQLiteDatabase db;
int pos;
Activity ac;
public CustomFehrestAdapter(Context context, Cursor c,Activity ac,SQLiteDatabase db) {
super(context, c);
this.ac =ac;
this.db = db;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.customfehrestitem,parent,false);
}
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
TextView item = (TextView) view.findViewById(R.id.fehresttxt);
favic = (ImageView) view.findViewById(R.id.favic);
pos = cursor.getPosition();
String title;
title = cursor.getString(cursor.getColumnIndex("titr1"));
final boolean isfav = checkFav(cursor);
if(isfav)favic.setImageResource(R.drawable.fav_ic);
else favic.setImageResource(R.drawable.favnot_ic);
item.setText(title);
final int idx = cursor.getInt(cursor.getColumnIndex("_id"));
// here is my image onClick handler.
favic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isfav){
db.execSQL("update content set favorite=1 where ID="+idx+";");
System.out.print("Add to favs");
fav.setImageResource(R.drawable.fav_ic);
}
else{
db.execSQL("update content set favorite=0 where ID=" + idx + ";");
System.out.print("Remove from favs");
fav.setImageResource(R.drawable.favnot_ic);
}
}
});
}
public boolean checkFav(Cursor cursor) {
int ppp = (int) favic.getTag();
cursor.moveToFirst();
cursor.moveToPosition(ppp);
int check = cursor.getInt(cursor.getColumnIndex("favorite"));
return check != 0;
}
}
Upvotes: 1
Views: 162
Reputation: 981
Try setting your onClickListener in the newView method .
The listView recycles or reuses its view for each listitem...say you have 1000 listitem.The adapter will only create a certain number of Views which fit on the screen and initialize it with data.So the newView will be called only for the number of Views visible on the screen .While the bindView will be called a 1000 times. You would not want your bindView being called each time..
public class CustomFehrestAdapter extends CursorAdapter {
String Tag;
SQLiteDatabase db;
int pos;
Activity ac;
public CustomFehrestAdapter(Context context, Cursor c, Activity ac, SQLiteDatabase db) {
super(context, c);
this.ac = ac;
this.db = db;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.customfehrestitem, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
return view;
}
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
String title = cursor.getString(cursor.getColumnIndex("titr1"));
boolean isfav = checkFav(cursor);
ViewHolder viewHolder = (ViewHolder) view.getTag();
if (isfav)
viewHolder.favic.setImageResource(R.drawable.fav_ic);
else
viewHolder.favic.setImageResource(R.drawable.favnot_ic);
viewHolder.item.setText(title);
final int idx = cursor.getInt(cursor.getColumnIndex("_id"));
// here is my image onClick handler
viewHolder.id = idx;
}
public boolean checkFav(Cursor cursor) {
int ppp = (int) favic.getTag();
cursor.moveToFirst();
cursor.moveToPosition(ppp);
int check = cursor.getInt(cursor.getColumnIndex("favorite"));
return check != 0;
}
public class ViewHolder implements View.OnClickListener {
TextView item;
ImageView favic;
int id = -1;
boolean isfav;
public ViewHolder(View view) {
item = view.findViewById(R.id.fehresttxt);
favic = (ImageView) view.findViewById(R.id.favic);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (id != -1) {
if (!isfav) {
db.execSQL("update content set favorite=1 where ID=" + idx + ";");
System.out.print("Add to favs");
favic.setImageResource(R.drawable.fav_ic);
} else {
db.execSQL("update content set favorite=0 where ID=" + idx + ";");
System.out.print("Remove from favs");
favic.setImageResource(R.drawable.favnot_ic);
}
}
}
}
}
Upvotes: 2