title: How to perform delete function on an item in shopping cart?
body: <p>I have created a remove button to remove the product from the shopping cart and it has successfully remove when I press the button. However, when I press back button and press the shopping cart button again, the item I remove is still there. How should I change my code so that even though I press back button and press back the shopping cart button my product is successfully remove?</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.shoppingcart);
  
      mCartList = ShoppingCartActivity.getCartList();
  
      // Make sure to clear the selections
      for(int i=0; i&lt;mCartList.size(); i++) {
          mCartList.get(i).selected = false;
      }
      System.out.println("This is it" + mCartList);
      System.out.println("This is all" + mProductAdapter);
  
      // Create the list
      final ListView listViewCatalog = (ListView) 
      findViewById(R.id.ListViewCatalog);
      mProductAdapter = new ProductAdapter(mCartList, getLayoutInflater(), 
      true, true, true);
      listViewCatalog.setAdapter(mProductAdapter);
  
      listViewCatalog.setOnItemClickListener(new OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int 
      position,
                                  long id) {
  
              product selectedProduct = mCartList.get(position);
              if (selectedProduct.selected == true)
                  selectedProduct.selected = false;
              else
                  selectedProduct.selected = true;
  
              mProductAdapter.notifyDataSetInvalidated();
  
          }
      });
  
      Button removeButton = (Button) findViewById(R.id.Button04);
      removeButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              // Loop through and remove all the products that are selected
              // Loop backwards so that the remove works correctly
              for (int i = getCartList().size() - 1; i &gt;= 0; i--) {
  
                  if (mCartList.get(i).selected) {
                      mCartList.remove(i);
                  }
              }
  
              double subTotal = 0;
              for(product p : mCartList) {
                  int quantity = ShoppingCartActivity.getProductQuantity(p);
                  subTotal += p.price * quantity;
              }
              TextView productPriceTextView = (TextView) 
        findViewById(R.id.TextViewSubtotal);
              productPriceTextView.setText("Subtotal: $" + subTotal);
  
              mProductAdapter.notifyDataSetChanged();
              System.out.println("This is it" + mCartList);
          }
      });
  
  public static void setQuantity(product product, int quantity) {
      // Get the current cart entry
      ShoppingCartEntry curEntry = cartMap.get(product);
  
      // If the quantity is zero or less, remove the products
      if(quantity &lt;= 0) {
          if(curEntry != null)
              removeProduct(product);
          return;
      }
  
      // If a current cart entry doesn't exist, create one
      if(curEntry == null) {
          curEntry = new ShoppingCartEntry(product, quantity);
          cartMap.put(product, curEntry);
          return;
      }
  
      // Update the quantity
      curEntry.setQuantity(quantity);
  }
  
  public static int getProductQuantity(product product) {
      // Get the current cart entry
      ShoppingCartEntry curEntry = cartMap.get(product);
  
      if(curEntry != null)
          return curEntry.getQuantity();
  
      return 0;
  }
  
  public static Map&lt;product, ShoppingCartEntry&gt; getCartMap() {
      return cartMap;
  }
  
  public static void removeProduct(product product) {
      cartMap.remove(product);
  }
  
  public static List&lt;product&gt; getCartList() {
      List&lt;product&gt; cartList = new Vector&lt;&gt;(cartMap.keySet().size());
      for(product p : cartMap.keySet()) {
          cartList.add(p);
      }
  
      return cartList;
  }
  
  public static int getCount() {
      int count =0;
      List&lt;product&gt; cartList = new Vector&lt;&gt;(cartMap.keySet().size());
      for(product p : cartMap.keySet()) {
          cartList.add(p);
          count++;
      }
      return count;
  }
  
  @Override
  protected void onResume() {
      super.onResume();
  
      // Refresh the data
      if(mProductAdapter != null) {
          mProductAdapter.notifyDataSetChanged();
      }
  
      double subTotal = 0;
      for(product p : mCartList) {
          int quantity = ShoppingCartActivity.getProductQuantity(p);
          subTotal += p.price * quantity;
      }
  
      TextView productPriceTextView = (TextView) 
   findViewById(R.id.TextViewSubtotal);
      productPriceTextView.setText("Subtotal: $" + subTotal);
  }
  </code></pre>