Op Kaushik
Op Kaushik

Reputation: 140

Text View does not get update through handler

Actually, I have designed a seprate Timer class with handler for displaying minute and second on a textview. this works fine as I have debugged it and handler will reaise handle but the text view does not get updated pl help me in this regards. I am tagging the code below for Timer Class. thanks in advance.

public class Timer
{
    private int _interval;
    public int getInterval()
    {
        return _interval;
    }
    public void setInterval(int delay)
    {
        _interval = delay;
    }
    private Handler handler;
    private Runnable _tickHandler;
    private Runnable delegate;
    private boolean ticking;
    public boolean getIsTicking()
    {
        return ticking;
    }
    public Timer(int interval)
    {
        _interval = interval;
        handler = new Handler();
    }
    public Timer(int interval, Runnable onTickHandler)
    {
        _interval = interval;
        setOnTickHandler(onTickHandler);
        handler = new Handler();
    }
    public void start(int interval, Runnable onTickHandler)
    {
        if (ticking) return;
        _interval = interval;
        setOnTickHandler(onTickHandler);
        handler.postDelayed(delegate, _interval);
        ticking = true;
    }
    public void start()
    {
        if (ticking) return;
        handler.postDelayed(delegate, _interval);
        ticking = true;
    }
    public void stop()
    {
        handler.removeCallbacks(delegate);
        ticking = false;
    }
    public void setOnTickHandler(Runnable onTickHandler)
    {
        if (onTickHandler == null)        return;
        _tickHandler = onTickHandler;
        delegate = new Runnable()
        {
            public void run()
            {
                if (_tickHandler == null) return;
                _tickHandler.run();
                handler.postDelayed(delegate, _interval);
            }
        }
        ;
    }
}

and below is the code that I am using for updating Text View

Timer tmr = new Timer(100,new Runnable()
{
    public void run()
    {
        long millis = System.currentTimeMillis() - mStartTime;
        int seconds = (int) (millis / 1000);
        int minutes = seconds / 60;
        seconds    = seconds % 60;
        lblDevOnOff.setText("");
        lblDevOnOff.setText(String.format("%d:%02d", minutes,seconds));
    }
}
);
tmr.start();

Upvotes: 0

Views: 579

Answers (2)

GedankenNebel
GedankenNebel

Reputation: 2587

Yeah, this is from the Android Dev Guide:

Thus, there are simply two rules to Android's single thread model:

  • Do not block the UI thread
  • Do not access the Android UI toolkit from outside the UI thread

Upvotes: 0

ngesh
ngesh

Reputation: 13501

put this code in

handler or runOnUIThread() something like this

runOnUIThread(new Runnable(){

public void run(){

lblDevOnOff.setText("");
lblDevOnOff.setText(String.format("%d:%02d", minutes,seconds));
}
})

you should do this because anything related to UI should run on UI Thread only...

Upvotes: 1

Related Questions