我现在有一个listview使用simplecursoradapter从数据库中获取数据,我查询数据库中的链接并将其分配给列表每行中的一个按钮,我正在重写simplecursoradapter类,为按钮设置onclicklistener,但我的代码不起作用,有人会告诉我这是什么问题吗? THX
这是我的适配器
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);
答案 0 :(得分:0)
尝试使用android.view.View.OnClickListener
@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);
holder.button.setOnClickListener(new android.view.View.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);
}
});
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
ChannelPoster tmpN=(ChannelPoster)items.get(position);
holder.text.setText(tmpN.getChannel());
holder.image.setImageResource(tmpN.getPoster());
return convertView;
}
答案 1 :(得分:0)
public class ChannelAdapter extends SimpleCursorAdapter implements OnClickListener {
@Override
public void onClick(View view) {
public void onClick(View view) {
int view_id = view.getId();
Log.d(THIS_FILE, "Im clicked....");
switch (view_id) {
case R.id.douban: {
dialFeedback.giveFeedback(ToneGenerator.TONE_DTMF_0);
keyPressed(KeyEvent.KEYCODE_0);
break;
}
}
}
}
答案 2 :(得分:0)