Tutorial :Android: Update Listview after Thread loads data from the net


  1. I like that my GUI appears immediately when the user starts the app.
  2. Then some data (text, pictures) gets loaded in the background (like YouTube app).
  3. The ListView and Gallery gets updated automatically with this new data.

I initiate my ListView, start a Thread and load the data... and then the ListView does not get updated! Several people told me I should use notifyDataSetChanged(). But I cannot place this command in my Thread (just unknown).

Any ideas?


I have this same problem... and I got excited when I came across this question. But no answer? :-(

After, letting the problem sit for about two weeks I found the solution here:

Long story short:

Quote from above link:

We must use a Handler object because we cannot update most UI objects while in a separate thread. When we send a message to the Handler it will get saved into a queue and get executed by the UI thread as soon as possible.

Once you check out the code you see get what the author is saying.
NOTE: Even with a handler, Android may not let you update a view object from the thread's run() method. I got this error:

05-31 02:12:17.064: ERROR/AndroidRuntime(881):  android.view.ViewRoot$CalledFromWrongThreadException:   Only the original thread that created a view hierarchy can touch its views.  

To get around it I updated an array of data in my run() method and used that array to update the view in the handler's handleMessage() method.

I hope this helps others out there.


You may use the slowAdapter to refresh the View:

SlowAdapter = new SlowAdapter(this);    list.setAdapter(SlowAdapter);  SlowAdapter.notifyDataSetChanged();  


Just found it myself while reading this thread and trying around.

Short: AsyncTask's method onProgressUpdate can touch the view: http://developer.android.com/reference/android/os/AsyncTask.html#onProgressUpdate(Progress...)

Background: I needed to call requery on my cursor so a ListView kept being updated while the task fills the database. The requery call made in doInBackground failed with the mentioned CalledFromWrongThreadException but same code in onProgressUpdate works.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »