Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the Main thread wait, when i dont know when will Async task finish the job.?

I am parsing the xml file containing some names using the Async task and populating these names to the listview again via the main thread. But whats happening in my case is, when the Async task is still running, the main thread is already populating the names to the listview which is resulting in no items on the listview. Should i make the main thread wait until the Async task finish the job or is there any other way to solve this prob.? if yes how can i make the main thread wait when i don't know that how long the Async task might take to finish.?

like image 678
suresh cheemalamudi Avatar asked Dec 01 '22 22:12

suresh cheemalamudi


1 Answers

If you want to complete the AsycTask then you can use .get() method from AsyncTask. (Which will block the MainUiThread)

But I think the best approach is without blocking MainUiThread you have to start ProgressDialog on onPreExecute() and after populating ListView in onPostExecute() of AsyncTask finish the ProgressDialog in onPostExecute().

like image 193
user370305 Avatar answered Dec 10 '22 12:12

user370305