Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 21st, 2012  |  syntax: None  |  size: 3.11 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to set onClickEvent for ImageButton in viewbinder?
  2. public class ChannelViewBinder implements SimpleCursorAdapter.ViewBinder {
  3.         public boolean setViewValue(View view, final Cursor cursor, int columnIndex) {
  4.  
  5.                 if(view instanceof ImageView) {
  6.                         ImageView iv = (ImageView) view;
  7.                         byte[] img = cursor.getBlob(columnIndex);
  8.                         iv.setImageBitmap(BitmapFactory.decodeByteArray(img, 0, img.length));
  9.                         return true;
  10.                 }
  11.  
  12.                 if(view instanceof ImageButton) {
  13.                         ImageButton ib = (ImageButton) view;
  14.                         ib.setOnClickListener(new  View.OnClickListener() {    
  15.                             @Override
  16.                             public void onClick(View v) {
  17.                                 String dblink = cursor.getString(cursor.getColumnIndex(ChannelDB.KEY_DBLINK));
  18.                                 Intent intent = new Intent();
  19.  
  20.                                 Bundle bundle = new Bundle();
  21.                                 bundle.putString("dblink",dblink);
  22.                                 intent.putExtras(bundle);
  23.                                 }
  24.                             });
  25.  
  26.                 }
  27.                 return false;
  28.         }
  29. }
  30.        
  31. public class ChannelPoster {
  32.     private Bitmap poster;
  33.     private String channel;
  34.     private String path;
  35.     private String dblink;
  36.  
  37.     public ChannelPoster(Bitmap pi, String c, String p, String d) {
  38.         poster = pi;
  39.         channel = c;
  40.         path = p;
  41.         dblink = d;
  42.     }
  43.  
  44.     public Bitmap getPoster() { return poster; }
  45.     public String getChannel() { return channel; }
  46.     public String getPath() { return path; }
  47.     public String getDBlink() { return dblink; }
  48. }
  49.        
  50. public void createchannelEntry(ChannelPoster channel) {
  51.         openDB();
  52.         ByteArrayOutputStream out = new ByteArrayOutputStream();
  53.         channel.getPoster().compress(Bitmap.CompressFormat.PNG, 100, out);
  54.         ContentValues cv = new ContentValues();
  55.         cv.put(KEY_POSTER, out.toByteArray());            
  56.         cv.put(KEY_CHANNEL, channel.getChannel());
  57.         cv.put(KEY_DBLINK, channel.getDBlink());
  58.         cv.put(KEY_PATH, channel.getPath());
  59.         mDb.insert(channelS_TABLE, null, cv);
  60.         closeDB();
  61.     }
  62.        
  63. ListView channellist = (ListView) findViewById(android.R.id.list);
  64.         mDB = new ChannelDB(this);
  65.  
  66.         String[] columns = {mDB.KEY_ID, mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_PATH, mDB.KEY_DBLINK};
  67.         String   table   = mDB.channelS_TABLE;
  68.  
  69.         Cursor c = mDB.getHandle().query(table, columns, null, null, null, null, null);
  70.  
  71.         startManagingCursor(c);
  72.  
  73.         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
  74.                 R.layout.channelview,
  75.                 c,
  76.                 new String[] {mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_DBLINK},
  77.                 new int[] {R.id.poster, R.id.channel, R.id.douban});
  78.  
  79.         adapter.setViewBinder(new ChannelViewBinder());
  80.  
  81.         channellist.setAdapter(adapter);
  82.        
  83. mDB.createchannelEntry(new ChannelPoster(image, "name" ,"link"  ,"link" ));