Question

I am having the following object in my activity class:

/**
 * 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;
    }
}

and I am using this object to update a ListView item. So I've set up a custom adapter for the list, to handle the current object, and add the data to the given view. Here is the code of my adapter:

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;
    }
}

and this is how I use it:

    /**
     * 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);

My problem is that in the method getView of my custom adapter, if I want to assign the object data to my view, the object property from that adapter, is always returning me out an object pointer(i think) instead of the object on wich I can call my getter methods to get the data. Here is a sample of what it is returned if I put a Log.e on it:

E/item﹕ com.avicolaarmeli.armeli.OrderItem@41561f98

Even if I typecast that into OrderItem or create a new var like: OrderItem item = object.get(position); I still cannot access the object's methods.

I've been trying to sort this out all the day and couldn't understand why. Can somebody please let me know what's wrong with my code?

Thanks

Was it helpful?

Solution

What you are seeing is the this.toString(), that is the default implementation of Object.toString(), since you are not overriding it.

add

@Override
public String toString() {
   return this.item_name != null ? this.item_name : "name not set";
}

to your OrderItem add see what difference it makes

@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;
    }

Also it should be

class OrderObjectAdapter extends ArrayAdapter<OrderItem>{

not

class OrderObjectAdapter<OrderItem> extends ArrayAdapter<OrderItem>{
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top