使用CursorAdapter,SetImageResource()在列表视图中工作不正常

时间:2016-04-09 04:03:05

标签: android listview adapter

我有一个列表视图,并为其附加自定义CursorAdapter。列表视图中的每个项目都有一个图像,我想在onClick发生时更改此图像。 例如,当我点击位置4的图像时,位置10的图像改变而4未改变。等等。 为什么会这样?

我的自定义CursorAdapter是:

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

1 个答案:

答案 0 :(得分:2)

尝试在newView方法中设置onClickListener。

listView为每个listitem回收或重用其视图...假设你有1000个listitem。适配器只会创建一定数量的视图,这些视图适合屏幕并用data初始化。所以newView只会被调用对于屏幕上可见的视图数量。而bindView将被调用1000次。 您不希望每次都调用bindView ..

公共类CustomFehrestAdapter扩展了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);
            }
        }
    }
}

}