1

I have an object of Order Items.it contains three values

1.item Name

2.Item Price

3.No of Items

I have a spinner, i have add this object on item selected.

When I add this object and select again , an other item it will also add into list, not override.

My code is below

    private OnItemSelectedListener itemSelectListener = new OnItemSelectedListener() {


    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
        // TODO Auto-generated method stub


        System.out.println("parent Tag:"+arg0.getTag());
        TextView itemName = (TextView)findViewById(Integer.parseInt(arg0.getTag().toString()));
        System.out.println("Item Name:" +itemName.getText().toString());
        System.out.println("Item Price:" +itemName.getTag().toString());
        System.out.println("Item QTY:" +arg0.getItemAtPosition(arg2).toString());
        orderItems = new OrderItems();          
        if(!"".equals(arg0.getItemAtPosition(arg2).toString())){
            orderItems.itemName = itemName.getText().toString();
            orderItems.itemPrice = itemName.getTag().toString();
            orderItems.noOfItems = Integer.parseInt(arg0.getItemAtPosition(arg2).toString());
            //if(itemsArray)
            itemsArray.additem(orderItems);
        }

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
};

public void additem(OrderItems object){
    itemArrayList.add(object);
}

can any body help me in this? What i am missing here?

Jamil
  • 5,457
  • 4
  • 26
  • 29
Nadeem
  • 51
  • 1
  • 1
  • 10
  • `itemsArray.additem(orderItems);` , you want to add in `orderItems` right ? – Suresh Atta Sep 01 '14 at 09:18
  • @SURESH ATTA I want to check if the selected item already exists then override it – Nadeem Sep 01 '14 at 09:20
  • @Caleryn I have edit my question you can check out. public void additem(OrderItems object){ itemArrayList.add(object); } this is my code – Nadeem Sep 01 '14 at 09:22
  • @Nadeem Ok so you are adding to the end of the ArrayList, this function will only ever add an object it explicitly allows for multiple copies of the same thing. If you wnat a single copy of each item you need a Set not a List you will also need to override both equals and hashcode on your OrderItems object – Caleryn Sep 01 '14 at 09:25
  • @Caleryn yes this is the problem – Nadeem Sep 01 '14 at 09:26
  • @Caleryn can you help me in code? – Nadeem Sep 01 '14 at 09:33

3 Answers3

1

Try this way,hope this will help you to solve your problem.

HashMap<Integer,Object> hashMap = new HashMap<Integer, Object>();
private OnItemSelectedListener itemSelectListener = new OnItemSelectedListener() {

   @Override
   public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {   
       System.out.println("parent Tag:"+arg0.getTag());
       TextView itemName = (TextView)findViewById(Integer.parseInt(arg0.getTag().toString()));
       System.out.println("Item Name:" +itemName.getText().toString());
       System.out.println("Item Price:" +itemName.getTag().toString());
       System.out.println("Item QTY:" +arg0.getItemAtPosition(arg2).toString());
       orderItems = new OrderItems();
       if(!"".equals(arg0.getItemAtPosition(arg2).toString())){
          orderItems.itemName = itemName.getText().toString();
          orderItems.itemPrice = itemName.getTag().toString();
          orderItems.noOfItems = Integer.parseInt(arg0.getItemAtPosition(arg2).toString());
          hashMap.put(arg2,orderItems);
       }
   }

   @Override
   public void onNothingSelected(AdapterView<?> arg0) {

   }
};
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • Glad to help you can you please upvote if it is useful to you ? – Haresh Chhelana Sep 01 '14 at 09:42
  • i tried to upvote but i have less reputations. But i have done as accepted answer. sorry for upvote – Nadeem Sep 01 '14 at 09:44
  • Thanks this worked for me. @HareshChhelana please do you think you could give me your opinion on this question http://stackoverflow.com/questions/25598696/recommended-way-order-to-read-data-from-a-webservice-parse-that-data-and-inse/25599152?noredirect=1#comment40016780_25599152 – Axel Sep 02 '14 at 03:34
0

if you want to check if the selected item is already in the itemArrayList you need to override the equals and hashcode function in your OrderItems class

class OrderItems{

//your methods

@Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        if (this == obj)
            return true;

        if (obj == null || (this.getClass() != obj.getClass())) {
            return false;
        }
        OrderItems order = (OrderItems) obj;
        return this.itemName.equals(order.getItemName())
                && this.itemPrice.equals(order.getItemPrice())
                && this.noOfItems.equals(order.getNoOfItems());
    }

    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return itemName != null ? itemName.hashCode() : 0;
    }
}

then in your onItemSelected function you can just use the contains method of the arraylist

         orderItems = new OrderItems(); 

        if(!itemArrayList.contains(arg0.getItemAtPosition(arg2))){
            orderItems.itemName = itemName.getText().toString();
            orderItems.itemPrice = itemName.getTag().toString();
            orderItems.noOfItems = Integer.parseInt(arg0.getItemAtPosition(arg2).toString());
            //if(itemsArray)
            itemsArray.additem(orderItems);
        }
Ker p pag
  • 1,568
  • 12
  • 25
0

Where you declare itemArrayList

 Set<OrderItems> itemArrayList = new HashSet<>();

The following two methods or equivalents dependant upon your needs should exist on the OrderItems object.

@Override
public int hashCode() {
    int hash = 43 * 3 + (this.itemName != null ? this.itemName.hashCode() : 0);
    hash = 43 * hash + (this.itemPrice != null ? this.itemPrice.hashCode() : 0);
    return 43 * hash + this.noOfItems;
}

@Override
public boolean equals(Object obj) {
    if (this == obj){
        return true;
    }
    if (obj == null || getClass() != obj.getClass()) {
        return false;
    }
    final OrderItems other = (OrderItems) obj;
    if ((this.itemName == null) ? (other.itemName != null) : !this.itemName.equals(other.itemName)) {
        return false;
    }
    if (this.itemPrice != other.itemPrice && (this.itemPrice == null || !this.itemPrice.equals(other.itemPrice))) {
        return false;
    }
    return this.noOfItems == other.noOfItems;
}

I have included all fields in both cases adjust both methods so that your equality condition (is when an OrderItems object is the same) is correct for BOTH functions.

the remainder of you r code should be close to unchanged.

I used an AutoGeneration algorithm to give me the equals and hashCode functions you may wish to do the same. rather than copy mine

Caleryn
  • 1,084
  • 3
  • 18
  • 23