在我的自定义ListView的事件监听器中,我需要从列表中的项目获取数据,但是需要从mainListView.getItemAtPosition(position)获取数据。 toString()返回类似这样的东西:com.package.object @ 1234556
这是对象的类
public class ItemPuesto {
protected long id;
protected String rutaImagen;
protected String nombre;
protected String tipo;
public ItemPuesto() {
this.nombre = "";
this.tipo = "";
this.rutaImagen = "";
}
public ItemPuesto(long id, String nombre, String tipo) {
this.id = id;
this.nombre = nombre;
this.tipo = tipo;
this.rutaImagen = "";
}
public ItemPuesto(long id, String nombre, String tipo, String rutaImagen) {
this.id = id;
this.nombre = nombre;
this.tipo = tipo;
this.rutaImagen = rutaImagen;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getRutaImagen() {
return rutaImagen;
}
public void setRutaImagen(String rutaImagen) {
this.rutaImagen = rutaImagen;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getTipo() {
return tipo;
}
public void setTipo(String tipo) {
this.tipo = tipo;
}
这是适配器:
public class ItemPuestoAdapter extends BaseAdapter {
protected Activity activity;
protected ArrayList<ItemPuesto> items;
public ItemPuestoAdapter(Activity activity, ArrayList<ItemPuesto> items) {
this.activity = activity;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return items.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.list_item_layout, null);
}
ItemPuesto item = items.get(position);
ImageView image = (ImageView) vi.findViewById(R.id.imagen);
int imageResource = activity.getResources().getIdentifier(item.getRutaImagen(), null, activity.getPackageName());
image.setImageDrawable(activity.getResources().getDrawable(imageResource));
TextView nombre = (TextView) vi.findViewById(R.id.nombre);
nombre.setText(item.getNombre());
TextView tipo = (TextView) vi.findViewById(R.id.tipo);
tipo.setText(item.getTipo());
return vi;
}
这是听众
mainListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{
//i.putExtra("seleccion", mainListView.getItemAtPosition(position).toString());
//startActivity(i);
alerta.setMessage(mainListView.getItemAtPosition(position).toString());
alerta.show();
}
});
答案 0 :(得分:0)
我的朋友,您从ItemPuesto
的对象获得了正确的输出。
toString()
函数返回对象的哈希值
如果您需要使用toString()
函数的特定输出,则必须覆盖toString()
类中的ItemPuesto
。
例如,您可以覆盖以获取nombre
的值,如下所示;
public String toString(){
return nombre;
}