2016-01-23 92 views
1

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

@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); 
} 
+0

ShoppingCartActivity.getCartList()如何检索产品?他们存储在本地数据库?如果你错过更新数据库。 – thetonrifles

+0

您需要管理您的背部按压方法。 –

+0

我将产品存储在java文件调用数据库帮助程序中,它们以列表<>存储,并且列表中有8个<>。 getCartList()从javafile列表中检索产品。 –

回答

0

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

​​
+0

不只是后面的新闻,但即使购物车按钮也表明,我没有删除产品,即使我做到了。我应该如何改变它? –

相关问题