我現在有一個使用simplecursoradapter從數據庫獲取數據的列表視圖,並且查詢數據庫中的鏈接並將其分配給列表中每一行的按鈕,並且我正在重寫simplecursoradapter類爲按鈕設置onclicklistener,但我的代碼不工作,有誰告訴我有什麼問題? THX如何在列表視圖中設置OnClickLitsener爲按鈕
這是我的適配器
public class ChannelAdapter extends SimpleCursorAdapter{
private LayoutInflater mInflater;
private List<ChannelPoster> items;
private Context mContext;
private String dblink;
public ChannelAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position,View convertView,ViewGroup par)
{
ViewHolder holder;
if(convertView == null)
{
convertView = mInflater.inflate(R.layout.channelview, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.poster);
holder.text = (TextView) convertView.findViewById(R.id.channel);
holder.button = (ImageButton) convertView.findViewById(R.id.douban);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
ChannelPoster tmpN=(ChannelPoster)items.get(position);
holder.text.setText(tmpN.getChannel());
holder.image.setImageResource(tmpN.getPoster());
holder.button.setOnClickListener(new ImageButton.OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(mContext, Doubanframe.class);
Bundle bunde = new Bundle();
bunde.putString("dblink",dblink);
intent.putExtras(bunde);
mContext.startActivity(intent);
}
});
return convertView;
}
private class ViewHolder
{
ImageView image;
TextView text;
ImageButton button;
}
}
,這是我如何填充ListView
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});
channellist.setAdapter(adapter);
你不初始化DBLINK – njzk2