0
votes

Just to be clear, this question is not about how to update a TextView from a Thread, that's working fine. The problem is that even if I make multiple calls to update the TextView throughout the Thread, the updates only appear after the Thread is done it's work. Here's an example:

public class NDThread extends Thread {

    protected LogActionListener log_listener;
    private Handler handler = new Handler();


    public void run() {
        logAction("starting");
        // Do many things..
        logAction("halfway");
        // Many more things..
        logAction("done");
    }

    public interface LogActionListener {
        public void onLogAction(String paramString);
    }

    public void logAction(final String str) {
        if(log_listener != null) handler.post(new Runnable() {
            @Override
            public void run() {
                log_listener.onLogAction(str);          
            }       
        });
    }
}

And in my main Activity, I implement LogActionListener to recieve the Strings and update the TextView:

NDThread thread = new NDThread();
thread.setOnLogActionListener(this);
thread.run();

// Elsewhere..
@Override
public void onLogAction(final String msg) {
        handler.post(new Runnable() {

            @Override
            public void run() {
                textView.append(msg);
            }

        });
}

As you can see, I've used Handlers in both the Thread and the Activity because I wasn't really sure which was correct to use. However, the result is always a blank TextView for the entirety of the Thread, then at the end it will print the 3 lines. What am I doing wrong?

4
Create a Handler in your activity rather than in your ThreadDharmendra
Using AsyncTask You can avoid messing with runOnUIThread and Handlers or other ugly looking codeRohit Sharma

4 Answers

2
votes

Avoid Thread and Go For AsyncTask

What you are looking for is onProgressUpdate(Progress...) , publishProgress(Progress...) of AsyncTask

Google for their code samples.

1
votes

Try using runOnUiThread and update textview inside it

0
votes

try replacing the definition of onLogAction() by:

@Override
public void onLogAction(final String msg) {
  textView.append(msg);
}
0
votes

The problem is here

NDThread thread = new NDThread();
thread.setOnLogActionListener(this);
thread.run();

It should be

NDThread thread = new NDThread();
thread.setOnLogActionListener(this);
thread.start();

Because "The start() method creates a new thread, that executes the run() method. The run() method just executes in the current thread, without starting a new thread."

Also as others mentioned already you dont need two handlers. Just do:

    public void logAction(final String str) {
        if(log_listener != null) log_listener.onLogAction(str);          
    }

and in main activity

    @Override
    public void onLogAction(final String msg) {
       MyActivity.this.runOnUiThread( new Runnable() {
          @Override
          public void run() {
           textView.append(msg);
          }
       });
    }