Android 3.0 introduced the Cursorloader implementation of asynchronous load data, in order to avoid the problem of blocking the UI thread when querying the database synchronously. Before API 11, you can download the support library to enable the previous system to support this feature, the download page is
Http://developer.android.com/tools/extras/support-library.html.
Here is an example:
Public classListviewloaderextendslistactivityImplementsLoadermanager.loadercallbacks<cursor> { //The Adapter being used to display the list ' s dataSimplecursoradapter Madapter; //These is the Contacts rows that we'll retrieve Static Finalstring[] PROJECTION =Newstring[] {contactscontract.data._id, ContactsContract.Data.DISPLAY_NAME}; //This is the select criteria Static FinalString SELECTION = "((" +ContactsContract.Data.DISPLAY_NAME+ "Notnull" and ("+ContactsContract.Data.DISPLAY_NAME+ "! =")) "; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Create a progress bar to display while the list loadsProgressBar ProgressBar =NewProgressBar ( This); Progressbar.setlayoutparams (Newlayoutparams (layoutparams.wrap_content, Layoutparams.wrap_content, Gravity.center)); Progressbar.setindeterminate (true); Getlistview (). Setemptyview (ProgressBar); //must add the progress bar to the root of the layoutViewGroup root =(ViewGroup) Findviewbyid (Android. R.id.content); Root.addview (ProgressBar); //for the cursor adapter, specify which columns go to which viewsstring[] Fromcolumns ={ContactsContract.Data.DISPLAY_NAME}; int[] Toviews = {Android. R.ID.TEXT1};//The TextView in Simple_list_item_1//Create An empty adapter we'll use to display the loaded data. //We pass NULL for the cursor and then update it in onloadfinished ()Madapter =NewSimplecursoradapter ( This, Android. R.layout.simple_list_item_1,NULL, Fromcolumns, Toviews,0); Setlistadapter (Madapter); //Prepare the loader. Either re-connect with an existing one,//or start a new one.Getloadermanager (). Initloader (0,NULL, This); } //called when a new Loader needs to be created PublicLoader<cursor> Oncreateloader (intID, Bundle args) { //Now Create and return a Cursorloader//creating a Cursor for the data being displayed. return NewCursorloader ( This, ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION,NULL,NULL); } //called when a previously created loader have finished loading Public voidOnloadfinished (loader<cursor>loader, Cursor data) { //Swap the new cursor in. (The framework would take care of closing the//Old cursor once we return.)madapter.swapcursor (data); } //called when a previously created loader is reset, making the data unavailable Public voidOnloaderreset (loader<cursor>loader) { //This was called when the last Cursor provided to onloadfinished ()//Above is on to being closed. We need to make sure we is no//longer using it.Madapter.swapcursor (NULL); } @Override Public voidOnlistitemclick (ListView L, View V,intPositionLongID) {//Do something if a list item is clicked }}
[Android Pro] uses Cursorloader to load data asynchronously from 3.0