new object created every time

70 views Asked by At

I'm developing a shopping cart app in android and a novice. I have been facing one problem now. I can add an item and it adds it to the cart. I can edit the quantity of an item or remove it from the listview after I have added it to the cart.

So what I want is to disable the addToCart button if it is present in the cart already in order to avoid duplicates. But every entry into a product is taken as a new entry. I think I have not referenced it properly. Any help would be appreciated.

This is the Activity that gets called every time you press an item (for example: Dell inside Laptops category)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.productdetails);
    LayoutInflater li;
    final List<Product> cart = ShoppingCartHelper.getCart();// get all //items from cart
    int productIndex = getIntent().getExtras().getInt(
            ShoppingCartHelper.PRODUCT_INDEX);// the item no in the list
    String PRODUCT_STRING = getIntent().getExtras().getString("PRODUCT");
    switch (PRODUCT_STRING) {
    case "Laptops":
        catalog = ShoppingCartHelper.getLaptopCatalog(getResources())
        break;
    case "Phones":
        catalog = ShoppingCartHelper
                .gePhonesCatalog(getResources());
        break;
    }
    final Product selectedProduct=(Product)this.catalog.get(productIndex);
// this declaration of product is taken as a new entry......................        
    ImageView productImageView = (ImageView) findViewById(R.id.ImageViewProduct);

    productImageView.setImageDrawable(selectedProduct.productImage);    TextView productTitleTextView = (TextView)findViewById(R.id.TextViewProductTitle);

    productTitleTextView.setText(selectedProduct.title);

    TextView productDetailsTextView = (TextView) findViewById(R.id.TextViewProductDetails);

    productDetailsTextView.setText(selectedProduct.description);

    final Button addToCartButton = (Button) findViewById(R.id.ButtonAddToCart);

    addToCartButton.setOnClickListener(new OnClickListener() {
        @Override

        public void onClick(View v) {
            cart.add(selectedProduct);
                selectedProduct.quantity ++;
            finish();
        }
    });     
    if(cart.contains(selectedProduct)) {
           addToCartButton.setEnabled(false);
           addToCartButton.setText("Item in Cart");
    }       
}
1

There are 1 answers

0
Hadi Satrio On

The fastest workaround to this would be to override the item's equals().

Otherwise, every instance of your item will be considered (and really is) different from Java's perspective.

Here's how you could do it:

// Somewhere in your item's class..

@Override
public boolean equals(Object o) {
    if(!(o instanceof YourItem)
        return false;
    YourItem i = (YourItem) o;
    // This line below is based on my assumption, you should change to better suit your usecase.
    return this.getItemId() == i.getItemId();
}

Of course in above code I assume that your item has a field for an item ID and a getter method to obtain it. Do note that it might not suit you and that you might have to make some adaptation to it.