我正在研究编写自定义适配器来填充每行3个textviews的listview。我发现了相当多的示例代码来做到这一点,但似乎最好的代码是:http://www.anddev.org/custom_widget_adapters-t1796.html无法在自定义适配器中使用LayoutInflater
经过一些小的调整,以解决最新的Android SDK的一些编译器问题,我得到它运行,只拿到了异常:
ERROR/AndroidRuntime(281):java.lang.UnsupportedOperationException:addView(查看,的LayoutParams)不支持的适配器视图
所以我做了很多的研究,发现了很多可能的原因和修复。没有一件事改变了。我的适配器代码当前是:
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;
}
}
所以我尝试了获取充气器的注释方式和当前未注释掉。我曾尝试通过“父”来夸大和空,并通过“真”,“假”,并完全省略最后一个参数。他们都没有工作,迄今为止我发现的所有例子都是从2008年开始的,我的感觉有些过时。
如果有人可以帮助解决这个问题,我很乐意解决这个问题。
当使用`AdapterView`时,你希望你的视图独立于父对象,`getView()`负责将它附加到父对象上。 – 2013-02-05 01:01:27