Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update ui from asynctask

I've seen many examples of how to do this, but I can't figure how to implement it in my code.

I am using this code.
I have updated the url, so it will receive a json with dynamic data. What I'm trying to do is to automatically update the list every 30 secs with this code.

Handler handler = new Handler();
    Runnable refresh = new Runnable() {
        public void run() {
            new GetContacts().execute();
            handler.postDelayed(refresh, 30000); 
        }
    };

It refreshes, and calls the url and gets the data, but the UI does not get updated.

Thanks for any hints to point me in the right direction.

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

like image 667
Bjorn Avatar asked Jun 01 '14 09:06

Bjorn


People also ask

What are different ways of updating UI from background thread?

In this case, to update the UI from a background thread, you can create a handler attached to the UI thread, and then post an action as a Runnable : Handler handler = new Handler(Looper. getMainLooper()); handler. post(new Runnable() { @Override public void run() { // update the ui from here } });

Which method is used to set the updates of UI?

Android Thread Updating the UI from a Background Thread The solution is to use the runOnUiThread() method, as it allows you to initiate code execution on the UI thread from a background Thread.

What replaces AsyncTask Android?

Alternative 1: Using Executor and Handler The executor will help in performing any task in the background and the handler will help to make UI changes.

Can I still use AsyncTask?

The AsyncTask API is deprecated in Android 11.


2 Answers

You have three protected methods in an AsyncTask that can interact with the UI.

  • onPreExecute()
    • runs before doInBackground()
  • onPostExecute()
    • runs after doInBackground() completes
  • onProgressUpdate()
    • this only runs when doInBackground() calls it with publishProgress()

If in your case the Task runs for a lot longer than the 30 seconds you want to refresh you would want to make use of onProgressUpdate() and publishProgress(). Otherwise onPostExecute() should do the trick.

See the official documentation for how to implement it.

like image 139
indivisible Avatar answered Sep 19 '22 12:09

indivisible


You can use AsycTask and update list ui on task finished within onPostExecute.

    new AsyncTask<String, String, String>() {
        /**
         * Before starting background do some work.
         * */
        @Override
        protected void onPreExecute() {
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO fetch url data do bg process.
            return null;
        }

        /**
         * Update list ui after process finished.
         */
        protected void onPostExecute(String result) {
               // NO NEED to use activity.runOnUiThread(), code execute here under UI thread. 

                 // Updating parsed JSON data into ListView
                 final List data = new Gson().fromJson(result);
                // updating listview
                ((ListActivity) activity).updateUI(data);
        }

    };
}

Update No need to use runOnUiThread inside onPostExecute, Because it's already called on and it's body executed under UIThread.

like image 21
Khaled Lela Avatar answered Sep 20 '22 12:09

Khaled Lela