i'm trying have progressdialog
appear when asynctask
starts in onpreexecute(), thing in doinbackground()
, , close progressdialog
in onpostexecute()
.
everywhere supposed easy , straight forward, must doing wrong.
whenever click button
in app update , asynctask
run app freezes up, waits bit, progressdialog
instantly appears , disappears, work in doinbackground()
completed. don't understand why onpreexecute()
being run after doinbackground()
, seems happening here. here code:
public class updatestats extends asynctask<catchapi, void, string[]> { private progressdialog pdia; private context context; private apiparser parser = new apiparser(); public updatestats(context contexts) { context = contexts; } @override protected void onpreexecute() { pdia = new progressdialog(context); pdia.setmessage("loading..."); pdia.show(); super.onpreexecute(); } @override protected string[] doinbackground(catchapi... api) { string apioutput = api[0].returnapi(); return new string[] { parser.getpoolname(apioutput), parser.gethashrate(apioutput), parser.getworkers(apioutput), parser.getsharesthisround(apioutput) }; } @override protected void onpostexecute(string[] result) { pdia.dismiss(); super.onpostexecute(result); } }
and onclicklistener in mainactivity:
updatebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string[] text = null; updatestats updater = new updatestats(context); try { text = updater.execute(api).get(); } catch (interruptedexception e) { e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); } poolnametv.settext(text[0]); hashratetv.settext(text[1]); workerstv.settext(text[2]); sharesthisroundtv.settext(text[3]); } });
in here :
text = updater.execute(api).get(); //<<<
you calling asynctask.get() freeze ui thread execution until doinbackground
execution not complete. execute asynctask :
updater.execute(api)
and update ui elements inside onpostexecute
called on ui thread when doinbackground
execution complete.
@override protected void onpostexecute(string[] result) { pdia.dismiss(); // update ui here.. poolnametv.settext(result[0]); hashratetv.settext(result[1]); workerstv.settext(result[2]); sharesthisroundtv.settext(result[3]); super.onpostexecute(result); }
Comments
Post a Comment