Android 实现从购物车中删除商品

Android 实现从购物车中删除商品,android,shopping-cart,Android,Shopping Cart,我正在尝试从我的应用程序购物车中实现项目删除(我使用了本教程) 将项目添加到购物车时,我要做的是将项目详细信息发送到购物车,cartActivity,然后在每个项目前面的购物车中设置编辑按钮。按下后,它进入编辑屏幕,并相应地加载所有数据。在editactivity中,我有删除按钮。我应该使用什么作为参考代码?如果是,我如何获得该代码? 添加的每个项目都有一个产品编号,如下面的屏幕截图所示 我使用了shoppingcarthalper.java public class ShoppingCart

我正在尝试从我的应用程序购物车中实现项目删除(我使用了本教程)

将项目添加到购物车时,我要做的是将项目详细信息发送到购物车,
cartActivity
,然后在每个项目前面的购物车中设置编辑按钮。按下后,它进入编辑屏幕,并相应地加载所有数据。在
editactivity
中,我有删除按钮。我应该使用什么作为参考代码?如果是,我如何获得该代码? 添加的每个项目都有一个产品编号,如下面的屏幕截图所示

我使用了
shoppingcarthalper.java

public class ShoppingCartHelper {

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

}

如果您在将项目的
id
CartActivity
传递到
EditActivity
时遇到问题,您可以使用
Intent
附加功能:

Intent intent = new Intent(this, EditActivity.class);
intent.putExtra("id", product.id); // id must be long
startActivity(intent);
EditActivity.java

Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    long id = getIntent().getLongExtra("id", 0);
    ...
}
public ShoppingCartEntry getByProduct(Product product) {
    return cartMap.get(product);
}
如果您无法获得
ShoppingCartEntry
实例,但您有
Product
实例,则可以将以下方法添加到
ShoppingCartHelper.java

Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    long id = getIntent().getLongExtra("id", 0);
    ...
}
public ShoppingCartEntry getByProduct(Product product) {
    return cartMap.get(product);
}

问题到底出在哪里?我不知道如何实现删除选项,我正试图找到一种方法来获取一些id到我的编辑屏幕,这样我就可以确保我已将相同的项目从购物车活动加载到editactivity。只需将其从cartMap中删除(我希望您使用Map来存储购物车内容)我已经用我的ShoppingCartHelper.java更新了这个问题,你介意告诉我删除按钮中应该包含什么样的代码吗。我已经使用过product,但是如何才能准确地获取所选项目我已经将getByProduct添加到ShoppingCartHelper.java,我接下来在cartActivity中应该做什么?您可以调用
ShoppingCartHelper.removeProduct(product)
括号内的产品为空,如何将所选项目分配给产品您如何知道,你想删除什么?您必须通过某种
id
获取产品实例。这意味着我需要将产品实例从cart ryt发送到编辑屏幕?