我坚持创建自定义适配器。我想在 ListView 内的按钮上设置 OnClickListener,我发现这个主题看起来不错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);