Java Thread Message Passing
- by pkulak
I'm writing an Android app. I have a main method, which creates and runs a new Thread using an anonymous inner Runnable class. The run() method, when it's done, calls a method on it's parent class (in the main thread) that calls notifyDataSetChanged() so that the main thread can redraw the new data. This is causing all kinds of trouble (ViewRoot$CalledFromWrongThreadException).
The thing is, this method being called from the worker thread is on the class that's created in the UI thread. Shouldn't that be running on the UI thread? Or am I missing something?
Here's some code about what I'm talking about:
public class Mealfire extends Activity {
@Override
public void onCreate(Bundle icicle) {
(new Thread() {
public void run() {
// Do a bunch of slow network stuff.
update();
}
}).start();
}
private void update() {
myAdapter.notifyDatasetChanged();
}
}