Android Loader 4: callback and complete example

Source: Internet
Author: User
Tags baseuri

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:

[Java] // 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
// Old cursor once we return .)
MAdapter. swapCursor (data );
}
// 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
// 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 ():

[Java] // This Adapter is used to display the list data.
SimpleCursorAdapter mAdapter;
...
 
Public void onLoaderReset (Loader <Cursor> loader ){
// The cursor used for the above onLoadFinished () will be executed when it is closed. We need to ensure that we no longer use it.
MAdapter. swapCursor (null );
}
// This Adapter is used to display the list data.
SimpleCursorAdapter mAdapter;
...

Public void onLoaderReset (Loader <Cursor> loader ){
// The cursor used for the above onLoadFinished () 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 function to display the content of a ListView containing the data returned from the contact contentprovider. It uses a CursorLoader to manage the query on the provider.


To obtain data from a user's contact, the manifest in this example must contain the READ_CONTACTS permission.

[Java] public static class CursorLoaderListFragment extends ListFragment
Implements OnQueryTextListener, LoaderManager. LoaderCallbacks <Cursor> {
 
// This is the Adapter used to display the List Data
SimpleCursorAdapter mAdapter;
 
// If it is not null, this is the current search Handler
String mCurFilter;
 
@ Override public void onActivityCreated (Bundle savedInstanceState ){
Super. onActivityCreated (savedInstanceState );
 
// If no data exists in the list, display the control text.
// In this application resource.
SetEmptyText ("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 (). initLoader (0, null, this );
}
 
@ Override public void onCreateOptionsMenu (Menu 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 (String newText ){
// Called when the search string on the action bar changes. Update
// Search for 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 don't care about this method
Return true;
}
 
@ Override public void onListItemClick (ListView l, View v, int position, long id ){
// Write the code you want to write
Log. I ("FragmentComplexList", "Item clicked:" + id );
}
 
// This is the row of data in the contact we want to obtain.
Static final String [] CONTACTS_SUMMARY_PROJECTION = new String [] {
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,
// Therefore, we do not need to care about the 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;
}
 
// Create now and return a CursorLoader, which will be responsible for creating
// Cursor is used to display data www.2cto.com
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 be concerned about closing the old cursor when we return it)
MAdapter. swapCursor (data );
}
 
Public void onLoaderReset (Loader <Cursor> loader ){
// Before the last Cursor is ready to enter the onLoadFinished () above.
// Cursor will be disabled. We need to make sure it is no longer used.
MAdapter. swapCursor (null );
}
}

From the column of nkmnkm

Related Article

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.