2

I have an android app where i have some set of text views which are required to change in real time. So please suggest me how to update the text view frequently.

Here is my code:-

protected void onResume() {
        // TODO Auto-generated method stub

        super.onResume();

        final Handler handler = new Handler();

    Thread thread =new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        Dt_textView.setText(//getting  the value from a class object);
                        As_textView.setText("getting  the value from a class object");
                    }
                }); 
            }

        });
    thread.start();
    }

This works but once the value gets change in the back end it doesn't get reflected immediately, until the resume function gets called again.

please give me a solution to resolve this, Its very important for me.

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138

3 Answers3

2

Try the following! It will get refreshed every second.

private Handler handler = new Handler();


private Runnable runnable = new Runnable() {

 public void run() {

        Dt_textView.setText(//getting  the value from a class object);
        As_textView.setText(""getting  the value from a class object);

  /*
   * Now register it for running next time
   */

  handler.postDelayed(this, 1000); // refresh every 1000 ms = 1 sec
 }


};

To run it use

runnable.run();

To stop it use

runnable.removeCallbacks(handler);
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
0

There is no way to reflect that immediately and without additional code. You need to call the setText manually after the value is "changed on the back". You may need to create a trigger or a "listener-style code".

gian1200
  • 3,670
  • 2
  • 30
  • 59
0

This works but once the value gets change in the back end it doesn't get reflected immediately

There is no way to trigger when your back end values get change.

until the resume function gets called again.

Activity's resume method call only when activity creates or when you navigate back to the activity. because of this you are able to update you textView only once.

please give me a solution to resolve this, Its very important for me.

The solution is to implement timer task to call your thread periodically you will find sample here How do you use a TimerTask to run a thread?

Hope it helps you to address your issue.

Community
  • 1
  • 1
vinay kumar
  • 1,451
  • 13
  • 33