-1

Hi Iam trying to update 3 textviews every second. I have written this piece of code. the thread starts normally but the textviews they do not get updated. I am passing a function inside the text parameters of the texts views that gets the current system time (using Calendar) in digits but then converts it to letters. example: for 3.45 THREE FORTYFIVE. any help would be appreciated. thanks

public class MainActivity extends Activity {

TextView currentv;
GetDate gd;
TextView currentmin;
TextView currentmins;
private Handler mHandler;
private boolean Running = true;







@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);





    currentv = (TextView) this.findViewById(R.id.tvtimehour);
    currentmin = (TextView) findViewById(R.id.tvtimemin);
    currentmins = (TextView) findViewById(R.id.tvtimesecond);



    gd = new GetDate();



    currentv.setText(gd.calculateTimeHour());
    currentmin.setText(gd.calculateTimeMinute());
    currentmins.setText(gd.calculateTimeMinuteDigit());



    mHandler = new Handler();
    Runnable runb = new Runnable(){

        @Override
        public void run(){
            while(Running == true){

                try{
                Thread.sleep(1000);
                }
                catch(Exception e){

                    e.printStackTrace();

                }

                mHandler.post(new Runnable(){
                    @Override
                    public void run(){


                             currentv.setText(gd.calculateTimeHour());
                     currentmin.setText(gd.calculateTimeMinute());
                                 currentmins.setText(gd.calculateTimeMinuteDigit());

                    }



                });


            }


        }





    };

    new Thread(runb).start();



}
user2799177
  • 63
  • 2
  • 11
  • 2
    possible duplicate of [Updating TextView every N seconds?](http://stackoverflow.com/questions/4776514/updating-textview-every-n-seconds) – ljs.dev Sep 20 '13 at 12:55

2 Answers2

2

Try this,

private MyTimerTask mytask;
private Timer timer;
mytask = new MyTimerTask();
timer = new Timer();
timer.schedule(mytask, 0,60000);

Timer class:

  class MyTimerTask extends TimerTask {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable() {
            public void run() {
                // Do your stuff here it will work
                           currentv.setText(gd.calculateTimeHour());
                           currentmin.setText(gd.calculateTimeMinute());
                           currentmins.setText(gd.calculateTimeMinuteDigit());
            }
        });

    }

}
Murali Ganesan
  • 2,925
  • 4
  • 20
  • 31
  • Hi Raja, thank you for the reply. I tried to do it this way, but it is not getting updated. when the system time changes the textview should reflect that change but it does not update no matter how long you wait. could it be that I am using a function inside the textview parameter. – user2799177 Sep 20 '13 at 13:17
  • After one minute this will execute. At the the you can get the device current time and set time. What ever you want – Murali Ganesan Sep 20 '13 at 13:26
1

Use Timer for this, I think because Thread can not update your UI.

Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44