2016-04-09 20 views
1

我有一个列表视图并附加一个自定义CursorAdapter它。列表视图中的每个项目都有一个图像,我想在发生onClick时更改此图像。 例如,当我用位置4单击图像时,位置10的图像发生了变化,而图像4未发生变化。等等。 为什么会发生这种情况?SetImageResource()在CursorAdapter的列表视图中不正确地工作

我定制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; 
} 
} 

回答

2

尝试设置你的onClickListener在NewView的方法。

listView为每个listitem回收或重新使用它的视图...假设您有1000个listitem。适配器只会创建一定数量的适合屏幕并使用data初始化的视图。因此,newView将被调用仅用于屏幕上可见的Views数量,而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); 
      } 
     } 
    } 
} 

}

+0

你的答案是正确的。但是需要为更新数据库点击了的项目的ID以及变更图像。我如何访问它? –

+0

favic.setOnClickListener在newView()方法中使用时不工作,我不知道为什么? –

+0

尝试使用viewHolder设计模式...检查了这为你的参考.... http://stackoverflow.com/questions/4567969/viewholder-pattern-correctly-implemented-in-custom-cursoradapter –