Android: text update in UI thread issue

I am very new to Android. I used Javabefore, but not about a year and a half. I'm having problems updating the screen, or rather, with TextView. I browsed the network for hours for solutions, and I know why it does not work, but I do not 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 turned off my program so that it just increments the int value on the screen for testing, and it still won't work. Instead, it simply displays "0". If I add a for loop before the method runOnUiThread(), it will increase the value of i, but I feel that it just increases the value and then displays it, rather than updating it in real time. Any help would be greatly appreciated.

+3
5

android, . ( ) Android. AsyncTask ...

donInBackground(), UI onProgressUpdate()... . AsyncTask

. ...

0

TextSwitcher

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

, . - .

 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();
+2

, mainComputations() . AsyncTask Android UI

0

, mainComputations , 0.

0

All Articles