【问题标题】:Deleting an item from the shopping cart从购物车中删除商品
【发布时间】:2015-07-05 14:40:43
【问题描述】:

我正在尝试从可展开的列表视图中删除一个对象。此代码在我的适配器类中。

                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;
    }

}

【问题讨论】:

  • 您将购物车中的物品存放在哪里?
  • selectedProduct = null 在这种情况下没用,您可能需要类似 catalog.removeProduct()
  • @Egor 我收到此错误消息,方法 removeProduct() 未定义为 ShoppingCartEntry 类型
  • @Blackbelt 我已经更新了我的代码,你能帮忙吗
  • @modabeckham,你得到这个错误并不奇怪,因为你没有指定这个方法。我的评论旨在指出您的产品删除逻辑是错误的,您需要重新考虑。

标签: android expandablelistview shopping-cart


【解决方案1】:

catalog.getProduct() 返回您想要的产品(或第一个,无论您的实施是什么)并将该值的副本分配给您的变量selectedProduct。通过给它一个空值,你就取消了产品的副本。

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

【讨论】:

    【解决方案2】:

    商品被添加回购物车的原因是您从未调用过将您的产品从购物车中删除的方法。您调用的唯一方法是从列表视图中删除该项目。我看到你有 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();
                    }
                });
    

    【讨论】:

    • 您需要创建一种方法来删除来自源的产品。产品保存在哪里?它保存在你的sqlite中吗?如果是这样,您需要创建一个从 sqlite 中删除产品的方法。然后确保在删除点击侦听器上调用该方法
    猜你喜欢
    • 1970-01-01
    • 2018-10-19
    • 2013-10-17
    • 2021-07-26
    • 1970-01-01
    • 2018-07-22
    • 2017-02-01
    • 1970-01-01
    相关资源
    最近更新 更多