Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating two consecutive textviews using threads
    primarykey
    data
    text
    <p>I have been reading up about threads in my java book and I wanted to try making updates with two consecutive threads at the same time.</p> <p>OF course though threads can't update the UI. I have used handlers a little bit but only by passing messengers from one class to another to capture location from a service to an activity.</p> <p>But this must be simpler than that.</p> <p>I assume I should use AsyncTask but i want to count 1-10 in each field and I don't want to keep dipping in and out of the asynctask class.</p> <p>Any pointers appreciated</p> <p>Stephen</p> <p><em>UPDATE</em></p> <p>So I did a little more experimenting. I wrote a handler method to handle any messages sent to it. It checks which field it should be updating and then performs a setText on said field. Here is the code</p> <pre><code>Handler myHandler = new Handler(){ public void handleMessage(Message msg){ msg = Message.obtain(); Bundle bundle = msg.getData(); String fieldName = bundle.getString("fieldName"); int count = bundle.getInt("count"); if(fieldName=="text1"){ text1.setText(count); }else{ text2.setText(count); } } }; </code></pre> <p>I then have some code in the onCreate of my activity which launches two threads and each thread passes a messages with my int count to the handler. It also passes in a TextView name so my handler knows which textview it should be updating. Here is the code...</p> <pre><code>public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text1 = (TextView)findViewById(R.id.text1); text2 = (TextView)findViewById(R.id.text2); new Thread(){ public void run(){ for(int i=1;i&lt;10;i++){ message = Message.obtain(myHandler); Bundle bundle = new Bundle(); bundle.putCharSequence("fieldName", "text1"); message.setData(bundle); myHandler.sendMessageDelayed(message, 1000); } } }.start(); new Thread(){ public void run(){ for(int i=1;i&lt;10;i++){ message = Message.obtain(myHandler); Bundle bundle = new Bundle(); bundle.putCharSequence("fieldName", "text2"); message.setData(bundle); myHandler.sendMessageDelayed(message, 1000); } } }.start(); } </code></pre> <p>But I am getting an error on the setText</p> <p>05-17 17:13:00.013: ERROR/AndroidRuntime(966): android.content.res.Resources$NotFoundException: String resource ID #0x0</p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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