4

我做了一個ListViewSimpleCursorAdapterViewBinder設置它的意見,我想把ImageButtonViewBinder但不知道如何設置onClick事件。我應該創建一個MySimpleCursorAdapter並放在那裏,或者我應該寫在ViewBinder課程中嗎?如何在ViewBinder中爲ImageButton設置onClick監聽器?

這裏是我的代碼:

ViewBinder.java

public class ChannelViewBinder implements SimpleCursorAdapter.ViewBinder { 
     public boolean setViewValue(View view, final Cursor cursor, int columnIndex) { 

       if(view instanceof ImageView) { 
         ImageView iv = (ImageView) view; 
         byte[] img = cursor.getBlob(columnIndex); 
         iv.setImageBitmap(BitmapFactory.decodeByteArray(img, 0, img.length)); 
         return true; 
       } 

       if(view instanceof ImageButton) { 
         ImageButton ib = (ImageButton) view; 
         ib.setOnClickListener(new View.OnClickListener() {  
          @Override 
          public void onClick(View v) { 
           String dblink = cursor.getString(cursor.getColumnIndex(ChannelDB.KEY_DBLINK)); 
           Intent intent = new Intent(); 

           Bundle bundle = new Bundle(); 
           bundle.putString("dblink",dblink); 
           intent.putExtras(bundle); 
           } 
          }); 

       } 
       return false; 
     } 
} 

ChannelPoster.java代表在ListView`的條目:

public class ChannelPoster { 
    private Bitmap poster; 
    private String channel; 
    private String path; 
    private String dblink; 

    public ChannelPoster(Bitmap pi, String c, String p, String d) { 
     poster = pi; 
     channel = c; 
     path = p; 
     dblink = d; 
    } 

    public Bitmap getPoster() { return poster; } 
    public String getChannel() { return channel; } 
    public String getPath() { return path; } 
    public String getDBlink() { return dblink; } 
} 

ChannelDB.java數據庫中的一個,我僅發佈有關部分:

public void createchannelEntry(ChannelPoster channel) { 
     openDB(); 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     channel.getPoster().compress(Bitmap.CompressFormat.PNG, 100, out); 
     ContentValues cv = new ContentValues(); 
     cv.put(KEY_POSTER, out.toByteArray());    
     cv.put(KEY_CHANNEL, channel.getChannel()); 
     cv.put(KEY_DBLINK, channel.getDBlink()); 
     cv.put(KEY_PATH, channel.getPath()); 
     mDb.insert(channelS_TABLE, null, cv); 
     closeDB(); 
    } 

最後名單,Tv.java

ListView channellist = (ListView) findViewById(android.R.id.list); 
     mDB = new ChannelDB(this); 

     String[] columns = {mDB.KEY_ID, mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_PATH, mDB.KEY_DBLINK}; 
     String table = mDB.channelS_TABLE; 

     Cursor c = mDB.getHandle().query(table, columns, null, null, null, null, null); 

     startManagingCursor(c); 

     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
       R.layout.channelview, 
       c, 
       new String[] {mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_DBLINK}, 
       new int[] {R.id.poster, R.id.channel, R.id.douban}); 

     adapter.setViewBinder(new ChannelViewBinder()); 

     channellist.setAdapter(adapter); 

這是我如何添加一個條目,如果有幫助:

mDB.createchannelEntry(new ChannelPoster(image, "name" ,"link" ,"link")); 

如果你需要更多的代碼,請告訴我。

回答

0

編輯:

抓抓我以前的答案。在連續切換最喜歡的明星之後向下滾動時出現錯誤。我想這與如何回收視圖或什麼有關。

相反,我還通過我的SQLite列favoritefrom,並在to明星ImageViewresource id。然後我extend SimpleCursorAdapter@Override bindView。我打電話super,然後使用view.findViewById獲得ImageView的句柄,其中view是傳遞到bindView的參數之一。使用該句柄,我可以有條件地設置適當的可繪製(星形填充或未填充),並設置clickListener

原來的答覆:

我的情況比較簡單,但類似的,所以我會後我做了什麼。我需要一顆能夠讓用戶喜歡一排的明星,所以我使用了ImageView。在我的from中,我傳遞SQLite列favorite,並在我的to中傳遞ImageViewresource Id

在我使用SimpleCursorAdapter.setViewBinder添加的SimpleCursorAdapter.ViewBinder()中,我覆蓋了setViewValue。然後我使用cursor.getColumnIndex("favorite")來測試傳入setViewValueindex值。如果相等,我將click listener設置爲view參數傳遞到setViewValue。根據我數據庫中favorite的值,我使用((ImageView) view).setImageResource()適當地切換ImageView。然後,我仍然在監聽器中更新數據庫中的值(親自使用OrmLite)。

不完全如此我想這樣做,但比擴展CursorAdapter和自己處理一切更容易,它似乎工作。

0

您不必擴展SimpleCursorAdapter,就可以將onClick事件放入您的ViewBinder類中。以下是我如何做到的:

private class MyViewBinder implements ViewBinder 
{ 
@Override 
public boolean setViewValue(View view, Cursor cursor, int columnIndex) 
{    
     if (columnIndex == cursor.getColumnIndex(COLUMN_NAME_CUSTOM)) 
     { 
      // If the column is COLUMN_NAME_CUSTOM then we use custom view. 
      // The following two lines are needed so that you can still click 
      // elsewhere in the list row to select it 
      view.setFocusable(false); 
      view.setFocusableInTouchMode(false); 
      // Set your onClickListener 
      view.setOnClickListener(new MyListener(someArgument)); 

      return true; 
     } 
     // For other columns, simply return false so that the default binding happens. 
     return false; 
    } 

    // Define your onclicklistener   
    private class MyListener implements OnClickListener 
    { 
     private String someArg = null; 

     // Constructor that lets you pass an argument to the listener 
     public MyListener(String someArg){ 
      this.someArg = someArg; 
     } 

     @Override 
     public void onClick(View v) { 
      //Handle your click event here 
     } 
    } 
}