3

I'm very new to Android. I've used Java before but not for around a year and a half. I'm having problems getting the screen to update, or rather the TextView. I have looked around the net for hours for solutions and I sort of know why its not working, but I don't know how to fix it.

public class PreliminaryActivity extends Activity {

//private static final int MENU_QUIT = Menu.FIRST;
int i = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    getWindow().setBackgroundDrawableResource(R.drawable.background);
    mainComputations();
}

    public void mainComputations(){
        runOnUiThread(new Runnable(){
        //@Override
            public void run(){

                TextView tv = (TextView) findViewById(R.id.time_display);

                tv.setText(new Integer(i).toString());
                i++;
            }
        });
    }

I've cut my program down so it should just increment an int value on the screen for testing and it still will not work. Instead it just displays '0'. If I add a for loop before the runOnUiThread() method, it will increment the i value but I have a feeling it is simply increasing the value then displaying it rather than it updating in real time. Any help would be greatly appreciated.

Nishant
  • 32,082
  • 5
  • 39
  • 53
Perilion
  • 43
  • 1
  • 4

5 Answers5

2

Go for TextSwitcher

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/TextSwitcher1.html

If you want your textview to update with some delay. use something like this.

 final int length = 10;
 Thread t = new Thread(new Runnable() {

     @Override
     public void run() {
        for(int i=0 ; i<length; i++) {
           runOnUiThread(new Runnable() {

              @Override
              public void run() {
                tv.setText(new Integer(i).toString());
              }
           }) ; 
           i++;
           Thread.sleep(500);
        }
     }
  });
  t.start();
Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72
  • the OP said... it was just for testing... I dont think one will do i++ in a separate thread... – Amit May 02 '12 at 12:25
  • In this case he will see always 1. – Martin Vandzura May 02 '12 at 12:28
  • only god knows what he want to display 0 or 1 or what – Rohit Sharma May 02 '12 at 12:29
  • That was purely for testing. My issue was that I couldn't figure out how to get the textview updating. It was only being called once, turns out the be the issue. Putting the i++ before or after the setText wouldnt really solve anything. Sorry for not making it clearer. – Perilion May 02 '12 at 12:33
0

In android, You can not update the UI from another thread. This is the restriction (which I consider a feature to remove unncecessary bugs) in android development. You can use AsyncTask for this...

In use, you can perform long task inside donInBackground() and than UI can be updated using onProgressUpdate()... see the AsyncTask example here

EDIT

see this similar question for more information...

Community
  • 1
  • 1
Amit
  • 13,134
  • 17
  • 77
  • 148
0

Your problem is because you call the mainComputations() only once. You should take a look at AsyncTask and the Android Developer resource Updating the UI from a Timer

Rajesh
  • 15,724
  • 7
  • 46
  • 95
  • Ah, In previous c++ or Java programs there has always been a main loop of sorts, I read that Android doesn't deal with them in the same way as adding a 'main loop' simply causes it to hang. I will take a look at the AsyncTask information. Cheers! – Perilion May 02 '12 at 12:29
0

You can not Update Main UI Thread from another thread for that you need to use Handler. Refer this link for more details

http://www.vogella.com/articles/AndroidPerformance/article.html

Krishnakant Dalal
  • 3,568
  • 7
  • 34
  • 62
0

What I see is that you call mainComputations only one time, so it is 0.

Martin Vandzura
  • 3,047
  • 1
  • 31
  • 63