Hello.
My name is Jarek and I've started to programm some stuff in the Android version of Java. Currently I was developing my applications in J2ME and now I'm trying to convert them to work on Android devices to use it's great features. So I've started to get familiar with the code and got stuck on a problem.
I'm trying to make a simple counter upwards to infinity when pressing a button. The loop works great in LogCat but somehow the TextView is never updated. When I remove the Thread.sleep() and use it to manually add 1 to "i" it works ok, just when trying to automate it something is not working.
Here is the java code :
Code:
package prog.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class startowe extends Activity
{
public int i;
public static TextView tabelka;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.startowe);
tabelka = (TextView) findViewById(R.id.wys2);
Button b = (Button) findViewById(R.id.btnClick);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
try {
startowe.this.wpisz();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void wpisz() throws Exception{
i = i+1;
System.out.println(""+i);
tabelka.setText(""+i);
Thread.sleep(5500);
startowe.this.wpisz();
}
}
And here is the XML layout file :
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/wys"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the first Screen"
/>
<Button
android:id ="@+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open New Screen"
/>
<TextView
android:id="@+id/wys2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
Any help is welcome. Thank You