android - How to update SQL database from within an onClick event of button inside a ListView -
how handle onclick
events of button within listview
item. fetching data of content provider in app. implemented custom simplecursoradapter
not update database within onclick
event. find below customsimplecursoradapter
code.
i warming android. might missing simple. appreciated
public class customsimplecursoradapter extends simplecursoradapter { private context mcontext; private context appcontext; private int layout; private cursor cr; private final layoutinflater inflater; public customsimplecursoradapter(context context, int layout, cursor c, string[] from, int[] to,int flag) { super(context, layout, c, from, to,flag); this.layout = layout; this.mcontext = context; this.inflater = layoutinflater.from(context); this.cr = c; } @override public view newview(context context, cursor cursor, viewgroup parent) { return inflater.inflate(layout, null); } @override public void bindview(view view, context context, cursor cursor) { super.bindview(view, context, cursor); textview name =(textview)view.findviewbyid(r.id.productname_value); final textview quantity = (textview) view.findviewbyid(r.id.availableinventory_value); textview price = (textview) view.findviewbyid(r.id.price_value); textview sales = (textview) view.findviewbyid(r.id.unitssold_value); button sellbutton =(button)view.findviewbyid(r.id.sellbutton); int name_index = cursor.getcolumnindexorthrow(inventorytable.column_name); int quantity_index = cursor.getcolumnindexorthrow(inventorytable.column_quantity); int price_index =cursor.getcolumnindexorthrow(inventorytable.column_price); int sales_index = cursor.getcolumnindexorthrow(inventorytable.column_sales); name.settext(cursor.getstring(name_index)); quantity.settext(cursor.getstring(quantity_index)); price.settext(cursor.getstring(price_index)); sales.settext(cursor.getstring(sales_index)); log.i("check before","check ok"); sellbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { uri produri = uri.parse(myinventorycontentprovider.content_uri + "/" + v.getid()); contentvalues values = new contentvalues(); string new_quantity = string.valueof(integer.parseint(quantity.gettext().tostring()) - 1); values.put(inventorytable.column_quantity,new_quantity); inventoryupdatetask().execute(myinventorycontentprovider.content_uri.tostring(), inventorytable.column_quantity, new_quantity, inventorytable.column_id, string.valueof(v.getid())); } }); } private class inventoryupdatetask extends asynctask<string, void, void> { private myinventorycontentprovider dbhelper = new myinventorycontentprovider(); @override protected void doinbackground(string... params) { contentvalues values = new contentvalues(); values.put(params[1], integer.parseint(params[2])); dbhelper.oncreate(); dbhelper.update(uri.parse(params[0]), values, params[3], new string[]{params[4]}); return null; } @override protected void onpostexecute(void result) { } } }
Comments
Post a Comment