Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: Threads and UI in Android Date: Sun, 03 Apr 2011 17:29:58 -0700 Organization: A noiseless patient Spider Lines: 47 Message-ID: References: <8vrrsdF6urU1@mid.individual.net> <8vs005F5tmU1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 4 Apr 2011 00:30:10 +0000 (UTC) Injection-Info: mx01.eternal-september.org; posting-host="3ILfXhSA8cUOvYVoDoGUlw"; logging-data="28943"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19OxNzYJwLWvB/vSfzKI5g5DSxn121J+1o=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 In-Reply-To: <8vs005F5tmU1@mid.individual.net> Cancel-Lock: sha1:Eb3DkxbCHcPHZPbPFcymm//wXLQ= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:2823 On 4/3/2011 12:27 PM, Dirk Bruere at NeoPax wrote: >> So far so good - no problems. >> However, if the Android ListView in the UI is to be updated with this >> data there is a problem. I cannot go in and do stuff to the ListView >> from BlinkAPI or I get a message about "called from wrong thread". This seems analogus to Java Swing & EDT. Use the Activity.runOnUiThread method to send processing to the UI thread, so you won't get this message. Given: some other thread | | V public void someMethod( Params.... ) { // do set up // do this on UI thread // do clean up } Transform this to: some other thread | | V public void someMethod( Params.... ) { // do set up Activity.runOnUiThread( new Runnable() { public void run() { // do this on UI thread } } ); // do clean up } To run the center bit of code on the UI thread. Note that you are executing code asynchronously and the "clean up" will likely happen before the UI thread bit. Something to be aware of.