我想在ListView
中显示包含多个列的数组。
调用View.getView()
时出现此错误:
android.widget.linerlayout无法强制转换为android.widget.textview
public class Lista extends Activity {
ListView lstProductos;
EditText txtNombre;
EditText txtEdad;
String message;
List<Detalle> Matriz_Detalle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista);
//lstDetalle = new ListView(this);
lstProductos = (ListView) findViewById(R.id.lstProductos);
Matriz_Detalle = new ArrayList<Detalle>();
// fill the ArrayList:
Matriz_Detalle.add(new Detalle("Name1", 25));
Matriz_Detalle.add(new Detalle("Name2", 30));
Matriz_Detalle.add(new Detalle("Name3", 40));
MyAdapter adapter = new MyAdapter(Matriz_Detalle);
// apply the Adapter:
lstProductos.setAdapter(adapter);
lstProductos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Detalle g = (Detalle) parent.getItemAtPosition(position);
Toast.makeText(
getApplicationContext(),
g.getName() + " is " + g.getAge() + " years old.",
Toast.LENGTH_LONG
).show();
}
});
}
private class MyAdapter extends BaseAdapter implements ListAdapter {
private final List<Detalle> data;
public MyAdapter(List<Detalle> data){
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView Nombre;
TextView Edad;
try{
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.prueba, null);
Nombre = (TextView) convertView.findViewById(R.id.txtNombre);
Edad = (TextView) convertView.findViewById(R.id.txtEdad);
}else{
Edad = (TextView) convertView;
Nombre = (TextView) convertView;
}
Edad.setText(data.get(position).name);
Nombre.setText(data.get(position).name);
} catch (Exception e) {
message = e.getMessage();
Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG).show();
}
return convertView;
}
}
private class Detalle{
private final String name;
private final int age;
public Detalle(String name, int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
答案 0 :(得分:1)
您尝试将convertView
投射到TextView
。
将您的getView()
更改为:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = vi.inflate(R.layout.prueba, null);
TextView = Nombre = (TextView) convertView.findViewById(R.id.txtNombre);
TextView Edad = (TextView) convertView.findViewById(R.id.txtEdad);
Edad.setText(data.get(position).name);
Nombre.setText(data.get(position).name);
return convertView;
}
答案 1 :(得分:0)
convertView
是ListView
的项目,因此当您尝试将其投放到TextView
或EditText
时,会发生ClassCastException
!
要重用convertView
,您应该创建一个内部类ViewHolder:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mHolder;
try{
if (convertView == null) {
mHolder = new ViewHolder();
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.prueba, null);
mHolder.nombre = (TextView) convertView.findViewById(R.id.txtNombre);
mHolder.edad = (TextView) convertView.findViewById(R.id.txtEdad);
convertView.setTag(mHolder);
}else{
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.edad.setText(data.get(position).name);
mHolder.nombre.setText(data.get(position).name);
} catch (Exception e) {
message = e.getMessage();
Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG).show();
}
return convertView;
}
class ViewHolder {
TextView nombre;
TextView edad;
}