From 7bc24a61d2b6077fb73f51d891681916c6afa186 Mon Sep 17 00:00:00 2001 From: Tony Vu Date: Sat, 25 Jun 2016 17:49:00 +0800 Subject: [PATCH] Add deletion option --- .../tonyvu/sc/demo/ShoppingCartActivity.java | 40 +++++++++++++++---- demo/src/main/res/values/strings.xml | 4 ++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/demo/src/main/java/com/android/tonyvu/sc/demo/ShoppingCartActivity.java b/demo/src/main/java/com/android/tonyvu/sc/demo/ShoppingCartActivity.java index bbd96ee..643fd84 100644 --- a/demo/src/main/java/com/android/tonyvu/sc/demo/ShoppingCartActivity.java +++ b/demo/src/main/java/com/android/tonyvu/sc/demo/ShoppingCartActivity.java @@ -1,11 +1,7 @@ package com.android.tonyvu.sc.demo; -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - +import android.app.AlertDialog; +import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; @@ -13,6 +9,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; +import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; @@ -25,6 +22,12 @@ import com.android.tonyvu.sc.model.Saleable; import com.android.tonyvu.sc.util.CartHelper; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + public class ShoppingCartActivity extends AppCompatActivity { private static final String TAG = "ShoppingCartActivity"; @@ -73,13 +76,36 @@ public void onClick(View v) { startActivity(intent); } }); + + lvCartItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + @Override + public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) { + new AlertDialog.Builder(ShoppingCartActivity.this) + .setTitle(getResources().getString(R.string.delete_item)) + .setMessage(getResources().getString(R.string.delete_item_message)) + .setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + List cartItems = getCartItems(cart); + cart.remove(cartItems.get(position-1).getProduct()); + cartItems.remove(position-1); + cartItemAdapter.updateCartItems(cartItems); + cartItemAdapter.notifyDataSetChanged(); + tvTotalPrice.setText(Constant.CURRENCY+String.valueOf(cart.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_UP))); + } + }) + .setNegativeButton(getResources().getString(R.string.no), null) + .show(); + return false; + } + }); } private List getCartItems(Cart cart) { List cartItems = new ArrayList(); Log.d(TAG, "Current shopping cart: " + cart); - Map itemMap = (Map) cart.getItemWithQuantity(); + Map itemMap = cart.getItemWithQuantity(); for (Entry entry : itemMap.entrySet()) { CartItem cartItem = new CartItem(); diff --git a/demo/src/main/res/values/strings.xml b/demo/src/main/res/values/strings.xml index 141ccba..e45b0e5 100644 --- a/demo/src/main/res/values/strings.xml +++ b/demo/src/main/res/values/strings.xml @@ -16,5 +16,9 @@ Remove Clear Cart Shop + Delete Item + Are you sure you want to delete this item? + Yes + No