0

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

1 Answer 1

1

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>{
Sign up to request clarification or add additional context in comments.

5 Comments

Well, ok, this returns my good key value. But I would like to get back the object to call it's methods. How can I achieve this?
I don't understand exactly what wasn't clear in my comment. Well, I have overrided the toString method, as you pointed out, and if I call object.toString() I get the item_name as in the object. so that's good. but for example, I have OrderItem item = (OrderItem) object.get(position); wich is actually a refference to the object. How can I call it's methods?
you mean outside the Adpater subclass?
In the class OrderObjectAdapter<OrderItem>, getView method, when I go and iterate trough the objects in the list that are of type OrderItem, I am getting the current object like this: OrderItem item = (OrderItem) object.get(position); but tha value of item is a string like this: com.avicolaarmeli.armeli.OrderItem@41561f98 instead of an OrderItem object on wich I can call methods. Why is that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.