2016-01-23 88 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

不只是後面的新聞,但即使購物車按鈕也表明,我沒有刪除產品,即使我做到了。我應該如何改變它? –

相關問題