我试图在应用购物车中实现删除选项。我已经实现了ShoppingCartHelper
并存储了项目详细信息。
这是我的产品项目对象在应用程序购物车中的样子。
我知道一旦用户按下删除按钮,我必须删除此对象。但我不知道如何从对象列表中删除所选对象。任何人都可以帮助我如何访问用户选择从项目列表中删除的特定项目。并且一旦找到item对象,然后如何删除特定对象。
对于大多数人来说,这可能是一个简单的问题,但请不要低估我,因为这是我的第一个Android应用程序。
Shoppingcart.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cart_activity);
mCartList = ShoppingCartHelper.getCartList();
expListView = (ExpandableListView) findViewById(R.id.lvExp);
mProductAdapter = new ProductAdapter(this, mCartList,
getLayoutInflater(), true);
expListView.setAdapter(mProductAdapter);
deleteCartButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
ShoppingCartHelper.java
public class ShoppingCartHelper {
public static final String PRODUCT_INDEX = "PRODUCT_INDEX";
private static Map<Product, ShoppingCartEntry> cartMap = new HashMap<Product, ShoppingCartEntry>();
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 void removeProduct(Product product) {
cartMap.remove(product);
}
public static List<Product> getCartList() {
List<Product> cartList = new Vector<Product>(cartMap.keySet().size());
for (Product p : cartMap.keySet()) {
cartList.add(p);
}
return cartList;
}
public static ShoppingCartEntry getByProduct(Product product) {
return cartMap.get(product);
}
}
ShoppingCartEntry.java
public class ShoppingCartEntry {
private Product mProduct;
private int mQuantity;
public ShoppingCartEntry(Product product, int quantity) {
mProduct = product;
mQuantity = quantity;
}
public Product getProduct() {
return mProduct;
}
public int getQuantity() {
return mQuantity;
}
public void setQuantity(int quantity) {
mQuantity = quantity;
}
}
ProductAdapter .java
public class ProductAdapter extends BaseExpandableListAdapter {
private List<Product> mCartList;
private Context _context;
private List<Product> _cartList;
private boolean mShowQuantity;
public ProductAdapter(Context context, List<Product> cartList,
LayoutInflater inflater, boolean showQuantity) {
this._context = context;
this._cartList = cartList;
mShowQuantity = showQuantity;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return _cartList.get(groupPosition).getItems().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return _cartList.get(groupPosition).getItems().get(childPosition)
.hashCode();
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.cart_list_item, parent, false);
}
TextView itemSize = (TextView) v.findViewById(R.id.lblListItemSize);
Item det = _cartList.get(groupPosition).getItems().get(childPosition);
itemSize.setText(det.itemName + " ( " + det.price + " ) ");
mCartList = ShoppingCartHelper.getCartList();
return v;
}
@Override
public int getChildrenCount(int groupPosition) {
int size = _cartList.get(groupPosition).getItems().size();
System.out.println("Child for group [" + groupPosition + "] is ["
+ size + "]");
return size;
}
@Override
public Object getGroup(int groupPosition) {
return this._cartList.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._cartList.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.cart_list_group, parent, false);
}
TextView groupName = (TextView) v.findViewById(R.id.lblListHeader);
TextView groupQty = (TextView) v.findViewById(R.id.lbl_qty);
TextView groupSubtotal = (TextView) v.findViewById(R.id.lblsubtotal);
final Product cat = _cartList.get(groupPosition);
groupName.setText(cat.description);
groupQty.setText(String.valueOf(cat.quantity));
groupSubtotal.setText(Double.toString(cat.subTotal));
TextView editTv = (TextView) v.findViewById(R.id.lblsubtotccal);
if (cat.itemCategory != null && cat.itemCategory.equals("Pizza"))
editTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent next = new Intent(_context, ActivityPizzaEdit.class);
Bundle b = new Bundle();
b.putDouble("subTotal", cat.subTotal);
next.putExtras(b);
next.putExtra("description", cat.description);
_context.startActivity(next);
((Activity) _context).overridePendingTransition(
R.anim.slide_in_right, R.anim.slide_out_left);
}
});
return v;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}