从购物车中删除商品

时间:2015-04-27 08:29:31

标签: android expandablelistview shopping-cart

我正在尝试从可扩展列表视图中删除对象。这段代码在我的适配器类中。

                deleteIB.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        ShoppingCartEntry catalog = ShoppingCartHelper.getByProduct(cat);
                        Product selectedProduct = catalog.getProduct();
                        selectedProduct  = null;
                        _cartList.remove(groupPosition);
                        ProductAdapter.this.notifyDataSetChanged();
                    }
                });

当我这样做时,它将从列表视图中删除该项目,但是再次打开购物车活动后,我可以在购物车中找到添加的项目。为什么它表现得那样?我是否需要在我的活动类中实现任何内容。

ShoppingCartHelper

public static final String PRODUCT_INDEX = "PRODUCT_INDEX";
    private static Map<Product, ShoppingCartEntry> cartMap = new HashMap<Product, ShoppingCartEntry>();

    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 void removeProduct(Product product) {
        cartMap.remove(product);
    }

    public static List<Product> getCartList() {
        List<Product> cartList = new Vector<Product>(cartMap.keySet().size());
        for (Product p : cartMap.keySet()) {
            cartList.add(p);
        }

        return cartList;
    }

    public static ShoppingCartEntry getByProduct(Product product) {
        return cartMap.get(product);
    }
}

ShoppingCartEntry

private Product mProduct;
    private int mQuantity;

    public ShoppingCartEntry(Product product, int quantity) {
        mProduct = product;
        mQuantity = quantity;
    }

    public Product getProduct() {
        return mProduct;
    }

    public int getQuantity() {
        return mQuantity;
    }

    public void setQuantity(int quantity) {
        mQuantity = quantity;
    }

}

2 个答案:

答案 0 :(得分:1)

catalog.getProduct()会返回您想要的产品(或者第一个,无论您的imlpementation是什么),并将该值的副本分配给您的变量selectedProduct。通过赋予它null值,可以使产品的副本无效。

你应该拨打catalog.removeProduct或类似的东西。请考虑使用ProductAdapter删除产品。目录将自动更新。

答案 1 :(得分:0)

将商品添加回购物车的原因是因为您从未调用过将商品从购物车中删除的方法。您调用的唯一方法是从列表视图中删除该项。我看到你有ShoppingCartHelper.removeProduct(selectedProduct)方法。你也应该这样打电话。

deleteIB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    ShoppingCartEntry catalog = ShoppingCartHelper.getByProduct(cat);
                    Product selectedProduct = catalog.getProduct();
                    ShoppingCartHelper.removeProduct(selectedProduct);
                    selectedProduct  = null;
                    _cartList.remove(groupPosition);
                    ProductAdapter.this.notifyDataSetChanged();
                }
            });