android - Show a ProgressBar while saving/showing a SQLite database -
i new android development.
trying complete application, app saving data sqlite database, showing, updating, deleting, etc. when try show rows database, takes time. want add progressbar show users it's taking time.
where in code can add progressbar?
this mainactivity:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //instantiate database handler db=new databasehandler(this); lv = (listview) findviewbyid(r.id.list1); pic = (imageview) findviewbyid(r.id.pic); nazev =(edittext) findviewbyid(r.id.nazev); objem =(edittext) findviewbyid(r.id.objem); obsah_alkoholu =(edittext) findviewbyid(r.id.obsah_alkoholu); aroma =(edittext) findviewbyid(r.id.aroma); chut =(edittext) findviewbyid(r.id.chut); dokonceni =(edittext) findviewbyid(r.id.dokonceni); poznamka =(edittext) findviewbyid(r.id.poznamka); vsechnyradky =(textview) findviewbyid(r.id.textview); showrecords(); } public void buttonclicked(view v){ int id=v.getid(); switch(id){ case r.id.save: if(nazev.gettext().tostring().trim().equals("")){ toast.maketext(getapplicationcontext(),"není název.", toast.length_long).show(); } else{ addrumy(); } showrecords(); break; case r.id.display: showrecords(); break; case r.id.pic: selectimage(); break; } } public void selectimage(){ intent photopickerintent = new intent(intent.action_pick); photopickerintent.settype("image/*"); startactivityforresult(photopickerintent, 2); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { switch(requestcode) { case 2: if(resultcode == result_ok){ uri choosenimage = data.getdata(); if(choosenimage !=null){ bp=decodeuri(choosenimage, 400); pic.setimagebitmap(bp); } } } } //convert , resize our image 400dp faster uploading our images db protected bitmap decodeuri(uri selectedimage, int required_size) { try { // decode image size bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; bitmapfactory.decodestream(getcontentresolver().openinputstream(selectedimage), null, o); // new size want scale // final int required_size = size; // find correct scale value. should power of 2. int width_tmp = o.outwidth, height_tmp = o.outheight; int scale = 1; while (true) { if (width_tmp / 2 < required_size || height_tmp / 2 < required_size) { break; } width_tmp /= 2; height_tmp /= 2; scale *= 2; } // decode insamplesize bitmapfactory.options o2 = new bitmapfactory.options(); o2.insamplesize = scale; return bitmapfactory.decodestream(getcontentresolver().openinputstream(selectedimage), null, o2); } catch (exception e){ e.printstacktrace(); } return null; } //convert bitmap bytes @targetapi(build.version_codes.honeycomb_mr1) private byte[] profileimage(bitmap b){ bytearrayoutputstream bos = new bytearrayoutputstream(); b.compress(bitmap.compressformat.png, 0, bos); return bos.tobytearray(); } // function values edittext , image private void getvalues(){ f_nazev = nazev.gettext().tostring(); f_objem = objem.gettext().tostring(); f_obsah_alkoholu = obsah_alkoholu.gettext().tostring(); f_aroma = aroma.gettext().tostring(); f_chut = chut.gettext().tostring(); f_dokonceni = dokonceni.gettext().tostring(); f_poznamka = poznamka.gettext().tostring(); photo = profileimage(bp); } //insert data database private void addrumy(){ getvalues(); db.addrumy(new rumy(f_nazev, f_objem, f_obsah_alkoholu, f_aroma, f_chut, f_dokonceni, f_poznamka, photo)); showprogressdialoghorizontal(); db.close(); } //retrieve data database , set list view private void showrecords(){ final arraylist<rumy> rumy = new arraylist<>(db.getallrumy()); data=new dataadapter(this, rumy); lv.setadapter(data); lv.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { datamodel = rumy.get(position); final dialog opendialog = new dialog(context2); opendialog.setcontentview(r.layout.dialogove_okno_mazani); alertdialog alertdialog = new alertdialog.builder(mainactivity.this).create(); alertdialog.settitle("souhrn záznamu k odeslání:"); alertdialog.setmessage("opravdu si přejete smazat tento záznam???"); alertdialog.setbutton(alertdialog.button_neutral, "zpět", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }); alertdialog.setbutton(alertdialog.button_negative, "zrušit mazání", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { startactivity(getintent()); } }); alertdialog.setbutton(alertdialog.button_positive, "smazat trvale!", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { // db.deleterumy(string.valueof(datamodel.getid())); showprogressdialoghorizontal(); db.cleardatabase(); db.close(); // showrecords(); finish(); // toast.maketext(getapplicationcontext(),string.valueof(datamodel.getid()), toast.length_short).show(); // startactivity(getintent()); } }); alertdialog.show(); } }); }}
db.getallrumy
public list<rumy> getallrumy() { list<rumy> rumytlist = new arraylist<rumy>(); // select query string selectquery = "select * " + table_name; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { rumy rumy = new rumy(); rumy.setid(integer.parseint(cursor.getstring(0))); rumy.setnazev(cursor.getstring(1)); rumy.setobjem(cursor.getstring(2)); rumy.setobsahalkoholu(cursor.getstring(3)); rumy.setaroma(cursor.getstring(4)); rumy.setchut(cursor.getstring(5)); rumy.setdokonceni(cursor.getstring(6)); rumy.setpoznamka(cursor.getstring(7)); rumy.setfoto(cursor.getblob(8)); // adding contact list rumytlist.add(rumy); } while (cursor.movetonext()); } // return contact list return rumytlist; }
use this
progressdialog pdialog = new progressdialog(this); pdialog.setmessage("please wait..."); pdialog.setprogressstyle(progressdialog.style_spinner); pdialog.setcancelable(false); pdialog.show();
and after data load dismiss progress dialog
pdialog.dismiss();
Comments
Post a Comment