【发布时间】:2011-05-17 10:04:20
【问题描述】:
我正在考虑编写一个自定义适配器来填充一个列表视图,每行有 3 个文本视图。我找到了很多示例代码来执行此操作,但似乎最好的一个是:http://www.anddev.org/custom_widget_adapters-t1796.html
在对最新的 Android SDK 的一些编译器问题进行了一些小调整后,我让它运行起来了,只是得到了异常:
ERROR/AndroidRuntime(281): java.lang.UnsupportedOperationException: addView(View, LayoutParams) 在 AdapterView 中不受支持
所以我做了很多研究,发现了很多可能的原因和解决方法。这些都没有改变任何事情。我的适配器代码目前是:
public class WeatherAdapter extends BaseAdapter {
private Context context;
private List<Weather> weatherList;
public WeatherAdapter(Context context, int rowResID,
List<Weather> weatherList ) {
this.context = context;
this.weatherList = weatherList;
}
public int getCount() {
return weatherList.size();
}
public Object getItem(int position) {
return weatherList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
Weather weather = weatherList.get(position);
//LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.weather_row, null, true);
TextView cityControl = (TextView)v.findViewById( R.id.city );
TextView temperatureControl = (TextView)v.findViewById( R.id.temperature );
ImageView skyControl = (ImageView)v.findViewById( R.id.sky );
return v;
}
}
所以我尝试了获取充气机的注释掉方法,以及当前未注释掉的方法。我曾尝试将“父级”传递给 inflate 以及 null,并传递“true”、“false”并完全省略最后一个参数。它们都没有奏效,到目前为止我发现的所有例子都是 2008 年的,我觉得有点过时了。
如果有人可以帮助解决这个问题,那么我很乐意解决这个问题。
【问题讨论】:
标签: android android-layout listview android-xml android-adapter