Skip to content

Commit

Permalink
Add deletion option
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyvu2014 committed Jun 25, 2016
1 parent 2d51979 commit 7bc24a6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
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;
import android.util.Log;
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;
Expand All @@ -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";

Expand Down Expand Up @@ -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<CartItem> 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<CartItem> getCartItems(Cart cart) {
List<CartItem> cartItems = new ArrayList<CartItem>();
Log.d(TAG, "Current shopping cart: " + cart);

Map<Saleable, Integer> itemMap = (Map<Saleable, Integer>) cart.getItemWithQuantity();
Map<Saleable, Integer> itemMap = cart.getItemWithQuantity();

for (Entry<Saleable, Integer> entry : itemMap.entrySet()) {
CartItem cartItem = new CartItem();
Expand Down
4 changes: 4 additions & 0 deletions demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@
<string name="remove">Remove</string>
<string name="clear_cart">Clear Cart</string>
<string name="shop">Shop</string>
<string name="delete_item">Delete Item</string>
<string name="delete_item_message">Are you sure you want to delete this item?</string>
<string name="yes">Yes</string>
<string name="no">No</string>

</resources>

0 comments on commit 7bc24a6

Please sign in to comment.