0

I have the following code which should update UI textview on Asynctask:

public class HelloWorldActivity extends Activity
{
    private static TextView txtview;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtview = (TextView) findViewById(R.id.mainview);
    }

    private static class SimpleTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... res) {
            try {
                Thread.sleep(1500);
            } catch(InterruptedException exception) {
                Thread.currentThread().interrupt();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void params) {
            txtview.setText("Hola Mundo");
        }

    }
}

Particularly I'm triying to change Hello World to Hola Mundo after 1,5 seconds passed. The problem is that while running the app the text still is Hello World and doesn't change. I get no error of any sort. I have even set txtview value outside onCreate method to avoid any access problems (or so I think).

If after txtview = (TextView) findViewById(R.id.mainview); I do txtview.setText("Hola Mundo"); then it works flawlessly.

What could be the problem?

Alper Turan
  • 1,220
  • 11
  • 24

3 Answers3

1

Forget to execute AsyncTask by calling AsyncTask.execute() method. do it as by adding following lines in onCreate method after initializing txtview TextView object:

SimpleTask objSimpleTask=new SimpleTask();
objSimpleTask.execute();
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • At first I put it on the main class and it threw a ` expected` error but your in-depth answer removed my doubt. It works, thanks! I'll upvote you sooner or later :) – Alper Turan Apr 16 '15 at 09:52
0

Currently you are forget to call AsyncTask.

But i think it is a bad practice to use AsyncTask & Thread.sleep() to update UI .

ou can simply do it with Handler.

Runnable updateUI;
Handler h = new Handler();

                    updateUI=new Runnable() {

                        @Override
                        public void run() {

                        txtview.setText("Hola Mundo");

                        }
                    };

                    h.postDelayed(updateUI,1500);
Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
  • Can you explain why it's bad practice to use AsyncTask & Thread.sleep() to update UI? do you have any articles I can read that explain it? Thanks. – Alper Turan Apr 16 '15 at 09:53
  • http://stackoverflow.com/questions/3956512/java-performance-issue-with-thread-sleep – Don Chakkappan Apr 16 '15 at 09:55
  • http://blogs.msmvps.com/peterritchie/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program/ – Don Chakkappan Apr 16 '15 at 09:55
  • Interesting. I'm now going to make use of Runnables and your links helped me research further the topic. One more question if you don't mind.. should I put the `Runnable` inside `onCreate` and it's going to create a thread that doesn't interfere with UI thread? I'm putting `h.postDelayed(updateUI,1500);` inside `onCreate` and everything else in the main class `HelloWorldActivity` but it gives error ` expected [javac] updateUI=new Runnable() {` – Alper Turan Apr 16 '15 at 10:25
  • I had to add `import android.os.Handler` and putting everything inside `onCreate` now works. – Alper Turan Apr 16 '15 at 10:39
  • @gizko feel free to accept the answer if you are satisfied with my answer – Don Chakkappan Apr 16 '15 at 10:40
0

You have not called ASyncTask, execute it like this after initializing TextView:

txtview = (TextView) findViewById(R.id.mainview);
SimpleTask objSimpleTask=new SimpleTask();
objSimpleTask.execute();

Hope is what you want.

Harsh Dattani
  • 2,109
  • 1
  • 17
  • 27
  • It throws a `cannot find symbol` error but `SimpleTask objSimpleTask = new SimpleTask(); objSimpleTask.execute();` works nonetheless. Still a great answer to understand the call should be put in the `onCreate` method. – Alper Turan Apr 16 '15 at 09:55
  • Thanks, I have edited the answer so can be helpful to others. – Harsh Dattani Apr 16 '15 at 09:57