我卡在创建自定义适配器上了。我想在ListView内的按钮上设置setOnClickListener,并找到了这个看起来不错的主题 how-to-setonclicklistener-on-the-button-inside-the-listview 但问题是我在getLayoutInflater行上得到了无法访问的代码错误。
这是我的代码:
public class MyCursorAdapter extends SimpleCursorAdapter{
private final Context ctx;
private Button tagButton = null;
public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
ctx = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = li.inflate(R.layout.tags_list_element, null, true);
tagButton= (Button)rowView.findViewById(R.id.tag_title);
tagButton.setTag(position);
tagButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
}
});
return rowView;
}
}
两种方法都对我不起作用。
LayoutInflater inflater = context.getLayoutInflater();
而且
LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);