Bir liste görünümüm var ve ona özel bir CursorAdapter
iliştir. Liste görünümündeki her öğenin, onClick
oluştuğunda bu resmi değiştirmek istediğim bir resmi vardır. Örneğin, 4 numaralı görüntüyü tıklattığımda, 10 konumunun resmi değişti ve 4 değişmedi. vb. Bu neden oluyor?SetImageResource(), CursorAdapter ile liste görünümünde yanlış çalışır
Benim özel CursorAdapter
geçerli:
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;
}
}
Yanıtınız doğru. ancak güncelleme veritabanı için tıklanan ve görüntüyü değiştiren öğenin kimliğine ihtiyacım var. Buna nasıl erişebilirim? –
favic.setOnClickListener newView() yönteminde kullanıldığında işe yaramadı ve nedenini bilmiyorum? –
viewHolder tasarım desenini kullanmayı deneyin ... referans için bu bağlantıyı kontrol edin .... http://stackoverflow.com/questions/4567969/viewholder-pattern-correctly-implemented-in-custom-cursoradapter –