我做了一個ListView
與SimpleCursorAdapter
和ViewBinder
設置它的意見,我想把ImageButton
ViewBinder
但不知道如何設置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"));
如果你需要更多的代碼,請告訴我。