I have a class which will store a list of objects and will also populate this list from a database (via a cursor) This will happen when I instantiate the object. I want to populate the list on a separate thread and then set a flag in the class when the list is populated. Do I need to use a handler or is that only for the UI thread?
Thanks,
m
This seems to work for me:
public void pop(){
mReadyToSearch = false;
new Thread() {
public void run() { final Long startTime = System.currentTimeMillis();
Log.i(TAG, "Start Pop");
populateAnimalListFromCursor();
mReadyToSearch = true;
final Long endTime = System.currentTimeMillis() - startTime;
Log.i(TAG, "End Pop : "+ Long.valueOf(endTime) +"ms");
}
}.start();
}
Seems a bit simple but it loads the list and sets the state of the class which is all I need at the minute. Can anyone see a problem with this?
Thanks, m
If you instantiate the object from somewhere on the UI-thread, the instantation will also be run on the UI-thread.
I suggest you add an AsyncTask (or a Runnable could do it, if you don’t need to update UI-components) as an inner class of your object to do the loading – this way it will be done from a non-UI-thread.