如何对购物车中的商品执行删除功能



我创建了一个删除按钮来从购物车中删除产品,当我按下按钮时,它已成功删除。但是,当我按下返回按钮并再次按下购物车按钮时,我删除的项目仍然存在。我应该如何更改我的代码,以便即使我按下返回按钮并按下购物车按钮,我的产品也已成功删除?

@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<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<?> 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 >= 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 <= 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<product, ShoppingCartEntry> getCartMap() {
    return cartMap;
}
public static void removeProduct(product product) {
    cartMap.remove(product);
}
public static List<product> getCartList() {
    List<product> cartList = new Vector<>(cartMap.keySet().size());
    for(product p : cartMap.keySet()) {
        cartList.add(p);
    }
    return cartList;
}
public static int getCount() {
    int count =0;
    List<product> cartList = new Vector<>(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);
}

如果重写 onBackPress 以重新启动当前活动,这将阻止撤消对数据库的任何更改。

@Override
public void onBackPressed() {
    startActivity(new Intent(this, MyActivity.class));
}

相关内容

  • 没有找到相关文章

最新更新