Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何对购物车中的商品执行删除功能?_Android - Fatal编程技术网

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

Android 如何对购物车中的商品执行删除功能?,android,Android,我已经创建了一个删除按钮来从购物车中删除产品,当我按下该按钮时,它已成功删除。然而,当我按下后退按钮并再次按下购物车按钮时,我移除的项目仍然存在。我应该如何更改我的代码,以便即使我按下后退按钮并按下后退购物车按钮,我的产品也能成功删除 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.s

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

@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);
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.shoppingcart);
mCartList=ShoppingCartActivity.getCartList();
//确保清除所选内容

对于(int i=0;i,如果您重写onBackPressed以重新启动当前活动,这将防止对数据库的任何更改被撤消

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

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

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

ShoppingCartActivity.getCartList()如何检索产品?它们存储在本地数据库中?以防您错过更新数据库。我将产品存储在java文件调用数据库帮助程序中,它们存储为列表,列表中有8个。getCartList()如何从java文件的列表检索产品。ShoppingCartActivity.getCartList()如何检索产品检索产品?它们存储在本地数据库中?以防您无法更新数据库。我将产品存储在java文件call database helper中,它们存储为列表,列表中有8个。getCartList()从javafile的列表中检索产品。不仅按back press键,甚至购物车按钮也显示我没有删除产品,即使我删除了。我应该如何更改它?不仅按back press键,甚至购物车按钮也显示我没有删除产品,即使我删除了。我应该如何更改它?