Android loader details 3: restart and callback

Source: Internet
Author: User
Restart Loader

When you useinitLoader()If the loader with the specified ID already exists, it uses this loader. if it does not exist, it will create a new one. but sometimes you want to discard the old data and start new data.

To discard old data, userestartLoader(). For example, the followingSearchView.OnQueryTextListenerThe loader is restarted when the user's query changes. Therefore, the loader needs to be restarted so that a new query can be conducted using the new search.

Public Boolean onquerytextchanged (string newtext) {// called when the search string in the action bar is changed. // update search overhead, and then restart the load to use this new overhead for new queries. mcurfilter =! Textutils. isempty (newtext )? Newtext: NULL; getloadermanager (). restartloader (0, null, this); Return true ;}Use loadermanager callback

LoaderManager.LoaderCallbacksIs a callback interface that allows the clientLoaderManagerInteraction.

Loader, which generally refersCursorLoaderWe want to keep the data after it is stopped. This allows the applicationonStop()AndonStart()So when the user returns to an application, they do not need to wait for the data to load.LoaderManager.LoaderCallbacksWhen necessary, create a new loader and tell the application when to stop using the loader data.

LoaderManager.LoaderCallbacksIncludes the following methods:

  • onCreateLoader()-Initialize and return a new Loader with the input ID.

  • onLoadFinished()-Called when a loader completes its loading process.

  • onLoaderReset()-Called when a loader is reset and its data is invalid.

Oncreateloader

When you try to operate a loader (for exampleinitLoader()) Will check whether the specified ID of the loader already exists. If it does not exist, it will triggerLoaderManager.LoaderCallbacksMethodonCreateLoader()This is where you create a new loader. Usually this loader isCursorLoaderBut you can also implement your own loader.

In the following example, the callback MethodonCreateLoader()CreateCursorLoader. You must use the constructor to createCursorLoader, The constructor needsContentProviderThe complete information of a query is used as a parameter. It is particularly required:

  • Uri-The URI of the content to be retrieved.

  • Projection-columns composed of columns to be returned are passed in.nullAll columns will be returned, but this is inefficient.

  • Selection-a filter that indicates which rows are returned. formatted like the sqlwhere Statement (except where ).nullAll rows will be returned.

  • Selectionargs-you can include some 'in selection '? ', It will be replaced by the value of this parameter. The order of these values appears and '? 'Sequence 1 in selection to. The value will be used as a string.

  • Sortorder-how to sort the rows. format it into a sample code similar to the sqlorder by statement (except for no orderby statement). Pass innullThe default sequence is used. The default sequence may be unordered.

Example:

// If non-null, this is the current filter the user has provided. string mcurfilter ;... public loader <cursor> oncreateloader (int id, bundle ARGs) {// This is called when you need to create a new loader. // We only have a simple loader, so we do not need to care about the ID. // first, pick the base URI to use depending on whether we are // currently filtering. 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 that will take care of // creating a cursor for the data being displayed. 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 ");}

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.