• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Set TextView from Thread

 
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a Thread that runs from the MainActivity and I'm wondering how I can update the UI from the result in the Thread?

The MainActivity looks like:


And in the SensorInterface Thread:


I would like to update the TextView with PPGresult

I tried adding to TestJNIActivity:


but nothing seems to happen.

Any ideas would be appreciated, thank you!
 
Saloon Keeper
Posts: 7073
165
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Extending Thread is really not the way to do concurrency these days (and never really was). I prefer AsyncTask for background operations, which has the onPostExecute method from where you can trigger UI updates.

As to your code, have you made sure that the run method is actually executed, and that the value of "PPGresult" is what you expect it to be? Put a log statement first thing in method that prints out its value.

I also note that you seem have two variables named "PPGresult", one in TestJNIActivity and one in SensorInterface. That's bound to cause confusion. It'll also be a source of concurrency issues due to variable visibility rules. At the least, declare variables accessed from different threads to be volatile.

As an aside, it is customary for Java variables to start with a lowercase letter, so "Register_Thread" should be "register_Thread", or even better "registerThread". Otherwise people looking at your code will assume that it is a class name. Same for "PPGresult".
 
Glenda Karen
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much! I'll look into using AsyncTask

Seems the run method is only being executed once when the app starts and then doesn't run again.

Thank you, I have changed it ppgResult and registerThread
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic