Onloadfinished
This method is called after the previously created loader has completed its loading process. this method is called before the data applied to the loader is released. in this method, you must delete all the use of old data (because it will be deleted soon), but do not release them by yourself, because their loaders will do these things.
Once you know that the application is no longer using data, the loader immediately releases the data. for example, if the data is a cursor from cursorloader, you should not call close () of the cursor (). if the cursor is placed in a cursoradapter, you should use the swapcursor () method so that the old cursor is not closed. for example:
// This adapter is used to display the list data. simplecursoradapter madapter ;... public void onloadfinished (loader <cursor> loader, cursor data) {// swap the new cursor in. (the Framework will take care of closing the // old cursor once we return .) madapter. swapcursor (data );}
Onloaderreset
This method is called when a created loader is reset to make its data invalid. this callback enables you to find out when the data will be stored by the cursor, so you can reference it by the cursor.
The following implementation calls swapcursor () with the parameter NULL ():
// This adapter is used to display the list data. simplecursoradapter madapter ;... public void onloaderreset (loader <cursor> loader) {// The cursor used for the onloadfinished () above will be executed when it is closed. We need to ensure that we no longer use it. madapter. swapcursor (null );}
Example
As an example, here we fully implement a fragment to display
Returns the content of the listview that queries data. It uses a cursorloader to manage the query of providers.
To obtain data from a user's contact, the manifest in this example must contain the read_contacts permission.
Public static class cursorloaderlistfragment extends listfragment implements onquerytextlistener, loadermanager. loadercallbacks <cursor> {// This is the adapter simplecursoradapter madapter used to display the list data; // if it is not null, this is the current searched handler string mcurfilter; @ override public void onactivitycreated (bundle savedinstancestate) {super. onactivitycreated (savedinstancestate); // if the list contains no data, display the control text. in a real application // This application resource is obtained. setempty Text ("No phone numbers"); // we have a menu item in the action bar. sethasoptionsmenu (true); // create an empty Adapter. We will use it to display the loaded data madapter = new simplecursoradapter (getactivity (), android. r. layout. simple_list_item_2, null, new string [] {contacts. display_name, contacts. contact_status}, new int [] {android. r. id. text1, android. r. id. text2}, 0); setlistadapter (madapter); // prepare loader. it may be to reconnect to an existing or start a new getloadermanager (). initloa Der (0, null, this) ;}@ override public void oncreateoptionsmenu (menu, menuinflater Inflater) {// place an action column for search. menuitem item = menu. add ("Search"); item. seticon (Android. r. drawable. ic_menu_search); item. setshowasaction (menuitem. show_as_action_if_room); searchview SV = new searchview (getactivity (); Sv. setonquerytextlistener (this); item. setactionview (SV);} public Boolean onquerytextchange (stri Ng newtext) {// when the search string in the action bar changes, it is called. Update // search the filter and restart loader to execute a new query mcurfilter =! Textutils. isempty (newtext )? Newtext: NULL; getloadermanager (). restartloader (0, null, this); Return true;} @ override public Boolean onquerytextsubmit (string query) {// we do not care about this method return true ;} @ override public void onlistitemclick (listview L, view V, int position, long ID) {// write the code log you want to write. I ("fragmentcomplexlist", "item clicked:" + id) ;}// this is the data of a row in the contact we want to obtain. static final string [] contacts_summary_projection = new strin G [] {contacts. _ id, contacts. display_name, contacts. contact_status, contacts. contact_presence, contacts. photo_id, contacts. lookup_key ,}; public loader <cursor> oncreateloader (int id, bundle ARGs) {// called when a new loader needs to be created. in this example, there is only one loader, // so we do not need to care about ID. first, set the base URI. The URI points to the contact URI baseuri; If (mcurfilter! = NULL) {baseuri = Uri. withappendedpath (contacts. content_filter_uri, Uri. encode (mcurfilter);} else {baseuri = contacts. content_uri;} // now create and return a cursorloader, which will create a // cursor for displaying data string select = "(" + contacts. display_name + "notnull) and (" + contacts. has_phone_number + "= 1) and (" + contacts. display_name + "! = '')"; Return New cursorloader (getactivity (), baseuri, contacts_summary_projection, select, null, contacts. display_name + "collate localized ASC");} public void onloadfinished (loader <cursor> loader, cursor data) {// Replace the new cursor. (the Framework will care about the closure of the old cursor when we return) madapter. swapcursor (data);} public void onloaderreset (loader <cursor> loader) {// before the last cursor is ready to enter onloadfinished () above. // cursor will be disabled. We need to make sure it is no longer used. madapter. swapcursor (null );}}