Android Api Demos The way to the Summit (45) loader--> Cursor

Source: Internet
Author: User
Tags baseuri

This demo demonstrates the use of class loader. About the use of class loader we have already introduced in the previous demo in this again small review.

Steps for using the class loader:
* 1. Get the class loader manager Loadermanager manager = Tent.getloadermanager ();
* 2. Initialize Loader
Mainactivity

 Public  class mainactivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Fragmentmanager fm = Getfragmentmanager ();//infer that if the view is not loaded in the activity, create a new view and load        if(Fm.findfragmentbyid (Android). r.id.content) = =NULL) {Cursorloaderlistfragment FRG =NewCursorloaderlistfragment (); Fm.begintransaction (). Add (Android.        R.id.content, FRG). commit (); }    } Public  class cursorloaderlistfragment extends listfragment implements Onquerytextlistener, oncloselistener {                    PrivateSimplecursoradapter Madapter;PrivateMysearchview Mysearchview;PrivateString Mcurrentfilter;@Override         Public void onactivitycreated(Bundle savedinstancestate) {Super. onactivitycreated (Savedinstancestate);When the query result is empty, the display prompts are set when no content is displayed in the ListViewSetemptytext ("No phone numbers!");//Set display ActionbarSethasoptionsmenu (true); Madapter =NewSimplecursoradapter (Getactivity (), Android. R.layout.simple_list_item_2,NULL,NewString[] {contacts.display_name, contacts.contact_status},New int[] {Android. R.id.text1, Android. R.ID.TEXT2},0); Setlistadapter (Madapter);//The list is not displayed at this time. Because the data is not ready.Setlistshown (false);//Get the Class loader manager and InitializeGetloadermanager (). Initloader (0,NULL, Myloader); }//define what to query        Private Finalstring[] Contacts_summary_projection =NewString[] {contacts._id, contacts.display_name, Contacts.contact_status, CONTACTS.CONTACT_PR Esence, contacts.photo_id, Contacts.lookup_key};PrivateLoadercallbacks<cursor> Myloader =NewLoadercallbacks<cursor> () {@Override             PublicLoader<cursor>Oncreateloader(intID, Bundle args) {Uri baseUri;if(Mcurrentfilter! =NULL) {//Assuming the query condition is not empty, append the query criteria on the original basisBaseUri = Uri.withappendedpath (Contacts.content_filter_uri, Uri.encode (Mcurrentfilter)); }Else{BaseUri = Contacts.content_uri; }//Define query criteriaString selection ="(("+ Contacts.display_name +"Notnull) and ("+ Contacts.has_phone_number +"=1) and ("+ Contacts.display_name +"! =")) ";return NewCursorloader (Getactivity (), BaseUri, Contacts_summary_projection, selection,NULL, Contacts.display_name +"COLLATE localized ASC"); }@Override             Public void onloadfinished(loader<cursor> Loader, cursor cursor) {//Load Complete Exchange query resultsMadapter.swapcursor (cursor);//Assume that the load animation is displayed when fragment is in the recovery state, otherwise the load animation is not displayed                //Data preparation complete, display list                if(Isresumed ()) {Setlistshown (true); }Else{Setlistshownnoanimation (true); }            }@Override             Public void Onloaderreset(loader<cursor> Loader) {//Reboot the loader first to ensure that the old data is purgedMadapter.swapcursor (NULL); }        };//define your own search view         Public  class mysearchview extends searchview {             Public Mysearchview(Context context) {Super(context); }//Under normal circumstances, the Serchview will not empty the contents when it shrinks .            //Here we need to rewrite the shrinkage method and empty the content when shrinking            @Override             Public void onactionviewcollapsed() {//Only update query content does not submit querySetquery ("",false);Super. onactionviewcollapsed (); }        }@Override         Public void Oncreateoptionsmenu(Menu menu, Menuinflater inflater) {Super. Oncreateoptionsmenu (menu, Inflater); MenuItem item = menu.add ("Search"); Item.seticon (Android. R.drawable.ic_menu_search);//Set how item is displayedItem.setshowasaction (Menuitem.show_as_action_if_room |            Menuitem.show_as_action_collapse_action_view); Mysearchview =NewMysearchview (Getactivity ());//monitor changes in query contentMysearchview.setonquerytextlistener ( This); Mysearchview.setoncloselistener ( This);//Set the search box to default to its own active zoom out iconMysearchview.seticonifiedbydefault (true);//Set the action view of item and just display the small icon before pressing. Press to expand the displayItem.setactionview (Mysearchview); }@Override         Public Boolean Onquerytextsubmit(String query) {//Do not care whether or not to submit. Processing has been made when the contents of the query have changed            return true; }@Override         Public Boolean Onquerytextchange(String NewText) {String Newfilter =! Textutils.isempty (NewText)?

NewText: null; //When the query condition does not change, do not do whatever it is, just return true.

if(Mcurrentfilter = =NULL&& Newfilter = =NULL) {return true; }if(Mcurrentfilter! =NULL&& mcurrentfilter.equals (Newfilter)) {return true; } mcurrentfilter = Newfilter;//Reboot loaderGetloadermanager (). Restartloader (0,NULL, Myloader);return true; }@Override Public Boolean OnClose() {//Empty the contents of the query if(! Textutils.isempty (Mysearchview.getquery ())) {Mysearchview.setquery (NULL,true); }return true; }@Override Public void Onlistitemclick(ListView L, View V,intPositionLongID) {Super. Onlistitemclick (L, V, position, id); Toast.maketext (Getactivity (),"Item clicked:"+ ID,0). Show (); } }}

Add permissions in the configuration file

<uses-permission android:name="android.permission.READ_CONTACTS"/>

Android Api Demos The way to the Summit (45) loader--&gt; Cursor

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.