I made a ListView with a SimpleCursorAdapter and a ViewBinder to set the views for it, and I want to put the ImageButton in the ViewBinder , but I donโt know how to set the onClick event. Should I create a MySimpleCursorAdapter and put it there, or should I write it in the ViewBinder class?
Here is my code:
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; } }
and ChannelPoster.java representing an entry in 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; } }
and ChannelDB.java in the database, I publish only part:
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(); }
and finally the list, 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);
Here's how I add an entry if it helps:
mDB.createchannelEntry(new ChannelPoster(image, "name" ,"link" ,"link" ));
If you need more code, just tell me.