sqlite - In ANDROID how delete data from LISTVIEW by Button click? -
in application listview populated data sqlite database. use button delete items. , modify table in sqlite db .
sql db class
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_table); tv = (textview) findviewbyid(r.id.txt_rates); // checkout = (button) findviewbyid(r.id.submit_btn); listcart = (listview) findviewbyid(r.id.list_item); pdialog = new progressdialog(this); ctx = this; cartdata = table.this.openorcreatedatabase("shopping_cart", mode_private, null); cartdata.execsql("create table if not exists cart_items(product_id varchar, name varchar, price varchar, quantity integer, model varchar, image varchar, manufacturer varchar )"); arraylist<cartproducts> mylist = new arraylist<cartproducts>(); cursor crsr = cartdata.rawquery("select * cart_items", null); final string[] productid = new string[crsr.getcount()]; final string[] productname = new string[crsr.getcount()]; final string[] productprice = new string[crsr.getcount()]; final string[] productquantity = new string[crsr.getcount()]; final string[] productmodel = new string[crsr.getcount()]; final string[] productimage = new string[crsr.getcount()]; final string[] productmanufacturer = new string[crsr.getcount()]; int j = 0; while (crsr.movetonext()) { string id = crsr.getstring(crsr.getcolumnindex("product_id")); productid[j] = id;//product_id,name,price,quantity,model,image,manufacturer name = crsr.getstring(crsr.getcolumnindex("name")); productname[j] = name; string price = crsr.getstring(crsr.getcolumnindex("price")); productprice[j] = price; string s = productprice[j].tostring(); s = s.replace(",", ""); string[] parts = s.split("\\."); // escape . string part1 = parts[0]; string part2 = parts[1]; part1 = part1.replace("₹", ""); total = integer.parseint(part1); // toast.maketext(table.this, part1, toast.length_short).show(); string qnty = crsr.getstring(crsr.getcolumnindex("quantity")); productquantity[j] = qnty; string s2 = productquantity[j].tostring(); total1 = integer.parseint(s2); sum = total * total1; string model = crsr.getstring(crsr.getcolumnindex("model")); productmodel[j] = model; string image = crsr.getstring(crsr.getcolumnindex("image")); productimage[j] = image; string manufacturer = crsr.getstring(crsr.getcolumnindex("manufacturer")); productmanufacturer[j] = manufacturer; //toast.maketext(getapplicationcontext(),productid[j]+""+productname[j]+""+productprice[j]+""+productquantity[j]+""+productmodel[j]+""+productimage[j]+""+productmanufacturer[j] , toast.length_short).show(); mylist.add(new cartproducts(productid[j], productname[j], productprice[j], productquantity[j], productmodel[j], productimage[j], productmanufacturer[j])); j++; gtot = gtot + sum; } listcart.setadapter(new cartproduct_listadapter(ctx, r.layout.activity_cart_row, mylist)); tv.settext("rs. " + string.valueof(gtot)); string s1 = productprice.tostring(); /*delete query*/ /* db.execsql("delete cart_items product_id='"+editrollno.gettext()+"'"); */ /* checkout.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent = new intent(getapplicationcontext(), product_details.class); startactivity(i); } }); */ }
adapter class
public class cartproduct_listadapter extends arrayadapter<cartproducts> { bitmap bitmap; imageview img; string urll; sqlitedatabase cartdata; sqliteopenhelper dbhelper; integer total = 0, quanty = 1, grandtot = 0, fulltotal = 0, = 0, sum; int it; private int resource; private layoutinflater inflater; private context context; public cartproduct_listadapter(context ctx, int resourceid, list<cartproducts> objects) { super(ctx, resourceid, objects); resource = resourceid; inflater = layoutinflater.from(ctx); context = ctx; } @override public view getview(int position, view convertview, viewgroup parent) { /* create new view of layout , inflate in row */ convertview = (relativelayout) inflater.inflate(resource, null); final cartproducts banqt = getitem(position); textview txtprice = (textview) convertview.findviewbyid(r.id.txt_item_price); txtprice.settext(banqt.getprice()); string s = txtprice.gettext().tostring(); s = s.replace(",", ""); string[] parts = s.split("\\."); // escape . string part1 = parts[0]; string part2 = parts[1]; part1 = part1.replace("₹", "");// toast.maketext(getcontext(), part1, toast.length_long).show(); total = integer.parseint(part1); edittext editqu = (edittext) convertview.findviewbyid(r.id.edt_qn); editqu.settext(banqt.getquantity()); quanty = integer.parseint(editqu.gettext().tostring()); grandtot = total * quanty; textview total = (textview) convertview.findviewbyid(r.id.txt_total); total.settext(string.valueof(grandtot)); button delet = (button) convertview.findviewbyid(r.id.btn_remove); delet.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { /*delete function*/ = integer.parseint(banqt.getproduct_id()); log.e("***i value******", string.valueof(it)); //toast.maketext(getcontext(), string.valueof(it), toast.length_long).show(); cartdata.delete("cart_items", "product_id='" + + "'", null); cartdata.close(); } }); textview txtname = (textview) convertview.findviewbyid(r.id.product_name); txtname.settext(banqt.getname()); img = (imageview) convertview.findviewbyid(r.id.img_product); urll = banqt.getimage().tostring(); urll = urll.replaceall(" ", "%20");// toast.maketext(getcontext(),urll,toast.length_long).show(); new loadimage().execute(urll); return convertview; } private class loadimage extends asynctask<string, string, bitmap> { @override protected void onpreexecute() { super.onpreexecute(); } protected bitmap doinbackground(string... args) { try { bitmap = bitmapfactory.decodestream((inputstream) new url(args[0]).getcontent()); } catch (exception e) { e.printstacktrace(); } return bitmap; } protected void onpostexecute(bitmap image) { if (image != null) { img.setimagebitmap(image); // pdialog.dismiss(); } else { // pdialog.dismiss(); toast.maketext(getcontext(), "image not exist or network error", toast.length_short).show(); } } } }
, layout im using cardview , im inflate cardview on layout
xml cardview code
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.cardview xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="290dp" android:id="@+id/cv" android:layout_margintop="10dp" android:layout_marginleft="10dp" android:layout_marginright="10dp" card_view:cardcornerradius="0dp" card_view:cardbackgroundcolor="#ffffff"> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/r1"> <imageview android:layout_width="80dp" android:layout_height="80dp" android:id="@+id/img_product" android:layout_centerhorizontal="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/product_name" android:layout_below="@id/img_product" android:textsize="18dp" android:text="parvana fancy necklace set" android:layout_margintop="25dp" android:textstyle="bold" android:inputtype="textmultiline" android:layout_centerhorizontal="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_item_price" android:text="12000 inr" android:textstyle="bold" android:textsize="15dp" android:gravity="center" android:layout_aligntop="@+id/edt_qn" android:layout_below="@id/product_name" android:layout_marginleft="25dp"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_availability" android:text="x" android:textstyle="bold" android:textsize="16dp" android:layout_aligntop="@+id/txt_item_price" android:layout_torightof="@+id/txt_item_price" android:layout_toendof="@+id/txt_item_price" android:layout_marginleft="10dp" android:layout_marginstart="54dp" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edt_qn" android:textsize="16dp" android:layout_torightof="@id/txt_availability" android:layout_below="@id/product_name" android:layout_margintop="18dp" android:layout_marginleft="10dp"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_eql" android:text="=" android:layout_torightof="@id/edt_qn" android:layout_below="@id/product_name" android:layout_margintop="22dp" android:layout_marginleft="10dp" android:textstyle="bold" android:textsize="16dp"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_total" android:text="rs.234" android:layout_torightof="@id/txt_eql" android:layout_below="@id/product_name" android:layout_margintop="25dp" android:layout_marginleft="10dp" android:textstyle="bold" android:textsize="15dp"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_remove" android:text=" delete" android:textsize="13dp" android:drawableleft="@drawable/delete_icon" android:layout_alignparentbottom="true" android:layout_alignleft="@+id/txt_availability" android:layout_alignstart="@+id/txt_availability" /> </relativelayout> </android.support.v7.widget.cardview>
this have done delete list row try in adapter class
delet.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { cartdata.updateselected(integer.parseint(cart_pdiscription[i]), 0); cartdata.deleteproduct(integer.parseint(cart_pdiscription[i])); intent intent = ((cartlist) context).getintent(); ((cartlist) context).finish(); context.startactivity(intent); } });
Comments
Post a Comment