我在我的活动类中有以下对象:
/**
* Item object
*/
class OrderItem {
/**
* Private params
*/
private String item_name;
private Double item_price;
private Integer quantity;
public void OrderItem(String name, Double price, Integer qt){
/**
* Init the object properties
*/
this.item_name = name;
this.item_price = price;
this.quantity = qt;
}
/**
* Getters and setters
*/
public String getName(){
return this.item_name;
}
public void setName(String name){
this.item_name = name;
}
public Double getPrice(){
return this.item_price;
}
public void setPrice(Double price){
this.item_price = price;
}
public Integer getQuantity(){
return this.quantity;
}
public void setQuantity(Integer qt){
this.quantity = qt;
}
}
我正在使用此对象更新 ListView 项。所以我为列表设置了一个自定义适配器,处理当前对象,并将数据添加到给定视图。这是我的适配器的代码:
class OrderObjectAdapter<OrderItem> extends ArrayAdapter<OrderItem>{
private final Context context;
private final List<OrderItem> object;
public OrderObjectAdapter(Context context,List<OrderItem> object){
super(context, R.layout.order_object_layout, object);
this.context = context;
this.object = object;
}
@Override
public View getView(int position, View conertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.order_object_layout, parent, false);
/** Gather items from the view */
TextView p_name = (TextView) rowView.findViewById(R.id.product_name);
TextView p_value = (TextView) rowView.findViewById(R.id.product_value);
TextView p_quantity = (TextView) rowView.findViewById(R.id.product_quantity);
/** Asign data to the text views */
OrderItem item = (OrderItem) object.get(position);
p_name.setText("");
return rowView;
}
}
这就是我使用它的方式:
/**
* Init the order_items adapter
*/
order_details_list_view = (ListView)findViewById(R.id.order_details_list_view);
order_items = new ArrayList<OrderItem>();
order_items_adapter = new OrderObjectAdapter(
this,order_items
);
/** Set the list adapter to the order items */
order_details_list_view.setAdapter(order_items_adapter);
我的问题是,在我的自定义适配器的 getView 方法中,如果我想将对象数据分配给我的视图,那么该适配器的 object 属性是总是让我退出一个对象指针(我认为),而不是我可以调用我的getter方法获取数据的对象。 以下是我在其上放置Log.e时返回的内容示例:
E/item﹕ com.avicolaarmeli.armeli.OrderItem@41561f98
即使我将其转换为OrderItem或创建一个新的var,如: OrderItem item = object.get(position); 我仍然无法访问该对象的方法。
我一直在努力解决这个问题,并且无法理解为什么。有人可以让我知道我的代码有什么问题吗?
由于
答案 0 :(得分:1)
你看到的是this.toString(),它是Object.toString()的默认实现,因为你没有覆盖它。
添加
@Override
public String toString() {
return this.item_name != null ? this.item_name : "name not set";
}
到OrderItem
添加,看看它有什么不同
@Override
public View getView(int position, View conertView, ViewGroup parent){
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.order_object_layout, parent, false);
}
/** Gather items from the view */
TextView p_name = (TextView) convertView.findViewById(R.id.product_name);
TextView p_value = (TextView) convertView.findViewById(R.id.product_value);
TextView p_quantity = (TextView) convertView.findViewById(R.id.product_quantity);
/** Asign data to the text views */
OrderItem item = (OrderItem) object.get(position);
p_name.setText(item.getName());
return convertView;
}
也应该是
class OrderObjectAdapter extends ArrayAdapter<OrderItem>{
不是
class OrderObjectAdapter<OrderItem> extends ArrayAdapter<OrderItem>{