android - Create and display a ListView of the ZXing ProductDatabase -
i created app scanning barcodes , qr code using zxing library. implemented database stores scanned products. need implement listview display stored products. ideas?
here classes:
barcodeactivity
@override public void oncreate (bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.spot_pay); button addbutton = (button) findviewbyid (r.id.addmenubutton); addbutton.setonclicklistener (new onclicklistener(){ public void onclick (view v){ startactivity(new intent(codicebarreactivity.this, aggiungicodiceactivity.class)); } }); } static final class productdata { string barcode; string format; string title; bigdecimal price; } } productdatabase:
private sqlitedatabase db; private static class productdatabasehelper extends sqliteopenhelper { public productdatabasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { stringbuilder sql = new stringbuilder(); sql.append("create table ").append(product_table) .append("( ") .append(" _id integer primary key,") .append(" barcode text,") .append(" format text,") .append(" title text,") .append(" price currency") .append(") "); db.execsql(sql.tostring()); log.d(tag, product_table + "table created"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + product_table); log.d(tag, product_table + "table dropped"); oncreate(db); } } public codicidatabase(context context) { productdatabasehelper helper = new productdatabasehelper(context); db = helper.getwritabledatabase(); } public boolean insert(productdata product) { contentvalues vals = new contentvalues(); vals.put("barcode", product.barcode); vals.put("format", product.format); vals.put("title", product.title); vals.put("price", product.price.multiply(one_hundred).longvalue()); return db.insert(product_table, null, vals) != -1; } } addproduct
private static final int request_barcode = 0; private static final productdata mproductdata = new productdata(); private edittext mbarcodeedit; private edittext mformatedit; private edittext mtitleedit; private edittext mpriceedit; private button mscanbutton; private button maddbutton; private codicidatabase mproductdb; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.add_product); mbarcodeedit = (edittext) findviewbyid(r.id.barcodeedit); mformatedit = (edittext) findviewbyid(r.id.codeformatedit); mtitleedit = (edittext) findviewbyid(r.id.titleedit); mpriceedit = (edittext) findviewbyid(r.id.priceedit); mscanbutton = (button) findviewbyid(r.id.scanbutton); mscanbutton.setonclicklistener(this); maddbutton = (button) findviewbyid(r.id.addbutton); maddbutton.setonclicklistener(this); mproductdb = new codicidatabase(this); // not yet shown } public void onclick(view v) { switch (v.getid()) { case r.id.scanbutton: intent intent = new intent ("com.google.zxing.client.android.scan"); intent.putextra("scan_mode", "product_mode"); startactivityforresult(intent, request_barcode); break; case r.id.addbutton: string barcode = mbarcodeedit.gettext().tostring(); string format = mformatedit.gettext().tostring(); string title = mtitleedit.gettext().tostring(); string price = mpriceedit.gettext().tostring(); string errors = validatefields(barcode, format, title, price); if (errors.length() > 0) { showinfodialog(this, "please fix errors", errors); } else { mproductdata.barcode = barcode; mproductdata.format = format; mproductdata.title = title; mproductdata.price = new bigdecimal(price); mproductdb.insert(mproductdata); showinfodialog(this, "success", "product saved successfully"); resetform(); } break; } } } private void resetform() { mbarcodeedit.gettext().clear(); mformatedit.gettext().clear(); mtitleedit.gettext().clear(); mpriceedit.gettext().clear(); } private void showinfodialog(context context, string title, string information) { new alertdialog.builder (context) .setmessage(information) .settitle(title) .setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }).show(); } public void onactivityresult(int requestcode, int resultcode, intent intent){ if (requestcode == request_barcode){ if (resultcode == result_ok) { string barcode = intent.getstringextra("scan_result"); mbarcodeedit.settext(barcode); string format = intent.getstringextra("scan_result_format"); mformatedit.settext(format); } else if (resultcode == result_canceled){ finish(); } } } } private static string validatefields(string barcode, string format, string title, string price) { stringbuilder errors = new stringbuilder(); if (barcode.matches("^\\s*$")) { errors.append("barcode required\n"); } if (format.matches("^\\s*$")) { errors.append("format required\n"); } if (title.matches("^\\s*$")) { errors.append("title required\n"); } if (!price.matches("^-?\\d+(.\\d+)?$")) { errors.append("need numeric price\n"); } return errors.tostring(); } }
overview of need do:
run query on database return cursor you. once you've got you'll have make make cursoradapter , override getview() method inflate , populate row views. after can use listview.setadapter() method passing in instance of adapter. handle updating list on screen whenever there new data.
i suggest instead of trying tackle in own project take break , go notepad tutorial developer docs. small , simple once complete have sample code use when working on doing barcode application.
Comments
Post a Comment