json - Android: Indeterminate progress bar for ServerCall routine. -
i'm working on simple i.m android app class makes queries server , displays results (i.e messages) on listview. 1 of requirements app display indefinite progress bar while query being made. means have set progressbar widget view.visible when query begins , view.invisible when query over. trouble i'm having query background function defined in different java class, while i'd this:
progressbar pb = (progressbar) findviewbyid(r.id.progressbar); pb.setvisibility(view.visible); ... ... uploader.execute(mycallspec); ... ... pb.setvisibility(view.invisible);
this doesn't work, because server call runs in background. using code, it's if progress bar never displayed.
how set progress bar displayed when service call begins , invisible when call completed?
go ahead asynctask
class mytask extends asynctask<void, void, void> { private progressbar pb; public mytask(progressbar pb) { this.pb = pb; } @override protected void onpreexecute() { super.onpreexecute(); pb.setvisibility(view.visible); } @override protected void doinbackground(void... params) { //your query here. return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); pb.setvisibility(view.gone); } } mytask mt = new mytask(pb); mt.execute()
but in real project suggest use robospice. asynctask doesn't handle activity lifecycle default.
upd:
there 2 ways have access progressbar instance inside asynctask:
- just declare inner class , save progressbar in field of outer (main) class
- pass reference via constructor. (see modified example)
Comments
Post a Comment