Sunday, May 30, 2010

Re: [android-developers] Thread and return values.

Robin van Leeuwen wrote:
> I have a time consuming task i don't want to run in the main UI thread,
> so i
> do the following (pseudo-code):
>
> onClick:
> getresults()
>
> public void getresults():
> TextView tv;
> String result = dolongjob()
> tv.setText(result);
>
> public void dolongjob():
> new Thread(){
> public void run(){
> // Some time consuming task
> // Which builds a String.
> }
> }
>
> Now as shown the i want the dolongjob, which builds a string, to return
> the calculated string
> to the caller (getresults), but since Thread->run() has to be returning
> void it can't return the
> calculated string.
>
> I know i have to be working with CountDownLatch.await to ensure 'tv' is
> only updated when the
> results are known, but how can i return a value which is calculated in a
> Thread?

You will be better served using an AsyncTask, doing your time-consuming
task in doInBackground() and calling tv.setText(theStringYouBuilt) in
onPostExecute().

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Training in US: 14-18 June 2010: http://bignerdranch.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate