Andrew McCandless
Lurker
Hi all,
I just started working with the Android SDK. I've read up on a bit of the literature at developer.android.com but coudln't wait to get my hands dirty writing some code. Well, dirty they are...
I'm trying to write a simple toy application that updates a string every second with the current time (unformatted). With the code below, my console prints out the current time every second, but the TextView doesn't. The TextView seems to update once at the beginning of the program, but otherwise stays the same. Sometimes I'll click the button and it will change once or twice, but that's about it.
I'm not really sure what's wrong here. The timer seems to be working fine, but not the TextView. I looked for something in the TextView that might allow me to force an instance of the TextView class to update, but could not find anything (which is what I would expect, since setText() should automatically update the TextView).
Anywho, my code is below. I'm using Android 2.1 (emulated). Any help would be greatly appreciated
I just started working with the Android SDK. I've read up on a bit of the literature at developer.android.com but coudln't wait to get my hands dirty writing some code. Well, dirty they are...
I'm trying to write a simple toy application that updates a string every second with the current time (unformatted). With the code below, my console prints out the current time every second, but the TextView doesn't. The TextView seems to update once at the beginning of the program, but otherwise stays the same. Sometimes I'll click the button and it will change once or twice, but that's about it.
I'm not really sure what's wrong here. The timer seems to be working fine, but not the TextView. I looked for something in the TextView that might allow me to force an instance of the TextView class to update, but could not find anything (which is what I would expect, since setText() should automatically update the TextView).
Anywho, my code is below. I'm using Android 2.1 (emulated). Any help would be greatly appreciated
Code:
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) this.findViewById(R.id.text);
Timer t = new Timer();
t.scheduleAtFixedRate(new TestTask(tv), 0, 1000);
}
class TestTask extends TimerTask{
TextView t;
public TestTask(TextView t){
this.t = t;
}
public void run(){
System.out.println("time = " + System.currentTimeMillis());
this.t.setText("time = " + System.currentTimeMillis());
}
}
}