android - Identify a specific object from list of object and delete the selected item -
i'm trying implement delete option in application cart. have implemented shoppingcarthelper
, stores item details.
this how product item object looks in application cart.
i know have delete object once user press delete button. don't know how delete selected object list of objects. can me how can access specific item user selected delete list of items. , once find item object how delete specific object.
this might easy question of please don't underestimate me since first android application.
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) { // current cart entry shoppingcartentry curentry = cartmap.get(product); // if quantity 0 or less, remove products if (quantity <= 0) { if (curentry != null) removeproduct(product); return; } // if current cart entry doesn't exist, create 1 if (curentry == null) { curentry = new shoppingcartentry(product, quantity); cartmap.put(product, curentry); return; } // update quantity curentry.setquantity(quantity); } public static int getproductquantity(product product) { // 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()); (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 group [" + groupposition + "] [" + 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; } }
Comments
Post a Comment