我创建了一个ViewBinder
, 来将我的项目保存在 aListView
中,它从 a 中获取其内容,SimpleCursorAdapter
并且在其中有一个ImageButton
. 我成功获得了列表,但ImageButton
只是不会响应我onclick
将字符串从数据库带到另一个的事件Activity
。这就是我遇到的问题。
public class ChannelViewBinder implements SimpleCursorAdapter.ViewBinder {
private ChannelDB mDB;
public boolean setViewValue(View view, final Cursor cursor, int columnIndex) {
final Context mContext = null;
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(mDB.KEY_DBLINK));
Intent intent = new Intent();
intent.setClass(mContext, Doubanframe.class);
Bundle bunde = new Bundle();
bunde.putString("dblink",dblink);
intent.putExtras(bunde);
}
});
}
return false;
}
}
下面是我的 MainActivity
课:
private Button likebutton;
private ImageButton about;
private ChannelDB mDB;
private ListView channellist;
private Cursor c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
likebutton=(Button) findViewById(R.id.share);
about =(ImageButton)findViewById(R.id.about);
channellist = (ListView) findViewById(R.id.Channel);
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;
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);
channellist.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
c.moveToPosition(position);
Intent intent = new Intent();
intent.setClass(HDtvs.this,Showlist.class);
intent .putExtra("path",mDB.KEY_PATH);
intent .putExtra("cname",mDB.KEY_CHANNEL);
intent .putExtra("dblink",mDB.KEY_DBLINK);
startActivity(intent);
}
});
}