2016-04-09 57 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 –