Question arising from my first attempt at using an Async object.
I have a main activity in which some TextViews have been created programmatically and added to a LinearLayout. Also a button, when this is clicked, an AsyncTask object is instantiated and results are obtained in the doInBackGround method. How should the result strings be transferred to the TextViews?
a) by calling the SetText methods of these TextViews from the onPostExecute method,
b) using intents and an onActivityResult method in the main activity
c) some other way (a clue would be nice!)
Thanks!
I would go for the
AsyncTaskoption. I’m guessing that as you already have one in place, the obtaining results part that happens when you click the button takes time, so it’s good design to have that in thedoInBackgroundmethod of theAsyncTask.Then you can call each
TextView‘ssetText(...)method in theonPostExecutemethod in yourAsyncTask. Or, it’s more suitable, you can update each view as you get the result by using thepublishProgress(...)andonProgressUpdate(...)methods (see the AsyncTask documentation) during the background calculations, instead of having to wait until the end.Just bear in mind that you can only call
setText(...)from theonPreExecute,onProgressUpdateandonPostExecutemethods, as (at least it seems this way from your explanation) the views have been created on the UI thread, so they can only be modified from that same thread, which those methods run on.