[Android Pro] uses Cursorloader to load data asynchronously from 3.0

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.