Estoy buscando escribir un adaptador personalizado para rellenar una vista de lista con 3 textviews por línea. Encontré bastante código de ejemplo para hacer esto, pero el que parecía el mejor era: http://www.anddev.org/custom_widget_adapters-t1796.htmlNo se puede usar LayoutInflater en el adaptador personalizado
Después de algunos retoques menores para solucionar algunos problemas del compilador con el último SDK de Android, lo ejecuté, sólo para obtener la excepción:
ERROR/Android Runtime (281): java.lang.UnsupportedOperationException: addView (Ver, LayoutParams) no se admite en AdapterView
Así que hice un montón de investigación y encontró muchas posibles razones y soluciones para esto. Nada de lo cual cambió nada. Mi código de adaptador es actualmente:
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;
}
}
Así que han tratado de la manera comentada de conseguir el inflado y el momento descomentado a cabo. He intentado pasar "padre" para inflar así como nulo, y pasar "verdadero", "falso" y omitir por completo el último parámetro. Ninguno de ellos ha funcionado, y todos los ejemplos que he encontrado hasta ahora han sido del 2008, y tengo la sensación de que están un poco desactualizados.
Si alguien puede ayudar con esto, me gustaría resolver el problema.
Al usar un 'AdapterView' desea que sus vistas sean independientes del padre,' getView() 'se encarga de adjuntarlo al padre por usted. –
@AustynMahoney No diría 'getView()' se encarga de ello porque ** usted ** es quien lo escribe, prefiero decir ** lo que llame a 'getView()' ** (en otras palabras, el marco) se encarga de adjuntar la vista ** devuelta por ** 'getView()'. – Joffrey