2

This question comes from an issue I am having, or really confusion on something. So its frowned upon to query databases on the UI thread. But my issue is, what if what appears on the screen relies on what the result is. Should you still run it in the background, or should you halt the screens output until you know the info you need from the database?

4

1 回答 1

4

The way you should set it up is to use an AsyncTask to query the database, using the doInBackground method. Then use the onPostExecute method to update the UI. You can't update the UI on any other thread than the main thread.

Hope that makes sense. If not, here are a couple articles that might help:

http://android-developers.blogspot.com/2009/05/painless-threading.html

http://developer.android.com/reference/android/os/AsyncTask.html

http://www.peachpit.com/articles/article.aspx?p=1823692&seqNum=3

Best of luck!

于 2012-07-12T00:00:39.363 回答