当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Run on UI thread updates only the last desired result (Android)

问题描述:

I am trying to do some task of updating the UI on the basis of elapsed time, following is my code:

My OnCreate method:

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TextView tx = (TextView)findViewById(R.id.textView);

startTime = System.nanoTime();

estimatedTime = (System.nanoTime() - startTime) / 1000000000;

tx.postInvalidate();

System.out.println(""+estimatedTime);

MainActivity.this.runOnUiThread(new Runnable(){

public void run() {

while (estimatedTime <= 100){

System.out.println(""+estimatedTime);

if(estimatedTime == 10){

TextView tx = (TextView)findViewById(R.id.textView);

tx.setText("Shri Ram Raksha Stotram");

System.out.println("Yay");

}

if(estimatedTime == 20){

TextView tx = (TextView)findViewById(R.id.textView);

tx.setText("Inatializing");

System.out.println("Yay");

}

if(estimatedTime == 30){

TextView tx = (TextView)findViewById(R.id.textView);

tx.setText("Preparing to install");

System.out.println("Yay");

}

if(estimatedTime == 40){

TextView tx = (TextView)findViewById(R.id.textView);

tx.setText("Installing");

System.out.println("Yay");

}

if(estimatedTime == 50){

TextView tx = (TextView)findViewById(R.id.textView);

tx.setText("Installed");

System.out.println("Yay");

}

if(estimatedTime == 60){

TextView tx = (TextView)findViewById(R.id.textView);

tx.setText("Unzipping packages...");

System.out.println("Yay");

}

estimatedTime = (System.nanoTime() - startTime) / 1000000000;

}

}

});

}

All of the above code is on the OnCreate method, however even after implementing a RunonUI thread I am not able to update the textview, the only result I get on the textview is the last if condition "Unzipping Packages". Where am I going wrong?

网友答案:

It's unzipping/installing too fast, it's the problem.

So if you want to see all log just append text with += operator to your textview if you want to see operations step by step then use Thread.Sleep(500); in your every if statements

分享给朋友:
您可能感兴趣的文章:
随机阅读: