views:

42

answers:

1

I am trying to make an application that can simulate a fight of two characters and I need to update some TextViews of their hp as it goes down. This is currently happening while two threads are running. Their hp is a global variable and since views can only be modified in the main thread, I have a while loop running while the threads are going that keeps updating the textviews. In Logcat, you can see the while loop running and the hp going down. However, the textviews are not updated until after the threads are finished.

I've tried using runnables and asynch task to update the textviews. None of these methods work. Why is this happening?

A: 

Hi! To update a view of the main activity from a thread you will have to pass a Handler to your thread. The Handler will allow you to send messages asynchronously to another thread. Then by switching on the message key you will be able to update your view with the value of the message. It's a mechanism of Android to pass data from Threads to threads.

I recommend you to check out the code from the lunarLander sample app from developer.android.com in the Resources section.

Here is a simpler example: http://www.helloandroid.com/tutorials/using-threads-and-progressdialog

Hope it helps. bye.

NioX5199