gpt4 book ai didi

java - 显示多列listview baseAdapter

转载 作者:行者123 更新时间:2023-12-01 11:39:09 25 4
gpt4 key购买 nike

我想在 ListView 中显示具有多列的数组。

调用 View.getView() 时出现此错误:

android.widget.linerlayout cannot be cast to 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;
}
}

最佳答案

您尝试将 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;
}

关于java - 显示多列listview baseAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29688015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com