tags:

views:

5087

answers:

3

Hi,

  1. I like that my GUI appears immediatly when the User starts the APP.
  2. Than some Data (text, Pictures) come loaded in the Background (like Youtube APP)
  3. The Listview and Gallery comes updated automaticly with this new Data.

I initiate my Listview, start a Thread and load the Data.... and than the Listview does not come updated!! Several People write I should use notifyDataSetChanged(). But I can not place this Command in my Thread (just unknown).

Any Ideas, It cant be so hard, but I come nearly crazy.. i will give you a Source Code if needed.

Greets Chris

+5  A: 

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: helloandroid.com/node/243

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.

CatDaaaady
A: 

You may used the slowAdapter to refresh the View.

SlowAdapter = new SlowAdapter(this);

list.setAdapter(SlowAdapter); SlowAdapter.notifyDataSetChanged();

raj
A: 

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.

digitarald