Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code is a bit confusing. In one case inside your primary <strong>while</strong> loop you are capturing the data, assigning it to a String variable <strong>s</strong> then using the text widget <strong>post()</strong> function and a <strong>Runnable</strong> to set the <strong>EditText</strong> widget to that value. But inside that same <strong>while</strong> loop you have exception handlers that simply set the same <strong>EditText</strong> widget directly. Your code also looks like you could potentially lose messages if the <strong>while</strong> loop resets the value of <strong>s</strong> before the timer loop has a chance to fire the set text call.</p> <p>It appears you are trying to create some form of real-time system and need the primary <strong>while</strong> loop to continually process, and display data as it becomes available. Now you have 3 different consumers (text widgets), but you didn't mention if you also have 3 different sources of messages or is there still only one main processing loop and some form of selector will decide which text widget gets the message?</p> <p>Were I building something along these lines, I would probably use a messaging system and follow the producer-consumer model. When text was received, I would have the primary processing loop push a simple 2-field message onto a queue that contained a reference to the text widget and a reference to the data string. Because Strings are immutable in Java, once the message object had its own copy of the text, any changes to <strong>s</strong> would not affect the message.</p> <p>Then, I would have a second thread running in the background that consumes the message queue. It would pull the message off the queue, construct a <strong>post</strong> call to the target text widget with the message data, fire it off, then go back to get the next message.</p> <p>By going this route you separate the data processing thread from the UI update processing and would not need to worry about how many text widgets or other widgets you need updated. If you ever need to add others you only need to worry about the new widgets being known to the code that creates the update messages. The thread doing the widget update doesn't know how many widgets you have, it simply uses the one referenced in the update message object that the message creator said to use.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload