Saturday, 15 March 2014

android - ProgressDialog in AsyncTask onPreExecute, but it appears after doInBackground -



android - ProgressDialog in AsyncTask onPreExecute, but it appears after doInBackground -

this part of application. (you can run code below isolated application) on website (url), using php language, parse numbers other website, , create array , encode json array, , show.

but, code below (without dismiss function!) progressdialog appears after doinbackground.

when add together dismiss function onpostexecute below, never appears. when set log checking dialog window, says there dialog.

i heard doinbackground freezes ui, freezes before dialog shown.

other similar questions have found solution, erasing .get() masynctask().execute().get(), don't have get() in code.

boolean variable loadfinish waiting finishing asynctask, show results website after asynctask. if delete while(loadfinish == false) there, automacally appears , disappears well, can't show result immediately...

add) found takes several seconds dialog appear after doinbackground... why?!

add2) i've tried move dialog codes before , after new masynctask().execute(), doesn't work too...

public class mainactivity extends activity { boolean loadfinish; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button start = (button) findviewbyid(r.id.start); //just button starting asynctask start.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { loadfinish = false; new masynctask().execute(); // , create json array java array while (loadfinish == false) ; } }); // add together array custom_list_data, , set custom row_adapter listview. // if delete while(loadfinish == false), array initial info added. } private class masynctask extends asynctask<string, string, string> { progressdialog dialog; @override protected void onpreexecute() { super.onpreexecute(); dialog = new progressdialog(mainactivity.this); dialog.setmessage("asdf"); dialog.setindeterminate(true); dialog.setcancelable(false); dialog.show(); } @override protected string doinbackground(string... params) { string url = "http://songdosiyak.url.ph/mouirate/etoos/3/main/"; string response_str = ""; httpclient client = new defaulthttpclient(); httpget request = new httpget(url); responsehandler<string> responsehandler = new basicresponsehandler(); seek { response_str = client.execute(request, responsehandler); } grab (clientprotocolexception e1) { e1.printstacktrace(); } grab (ioexception e1) { e1.printstacktrace(); } loadfinish = true; homecoming null; } @override protected void onpostexecute(string result) { super.onpostexecute(result); dialog.dismiss(); } } }

sorry poor english language language skill , give thanks reading!

as gabe mentioned don't need loop, need more understanding async methods should do.

you introduced result, because want display result. have homecoming response_str in doinbackground. available param onpostexecute can display it, or whatever need it.

so summarize:

remove loop return value response_str or whatever doinbackground display value in onpostexecute and remove loadfinish variable not needed @ all

hope helps.

android android-asynctask progressdialog

No comments:

Post a Comment