Android loader-Example

Source: Internet
Author: User
Tags baseuri

This example shows the complete implementation of a listview in fragment. The listview contains the query results for the address book content provider. It uses a cursorloader to manage queries based on the content provider.

Because this application accesses the user's address book, its list file must contain the read_contacts license.

Public static class cursorloaderlistfragment extends listfragment
Implements onquerytextlistener, loadermanager. loadercallbacks <cursor> {

// This is the adapter being used to display the list's data.
Simplecursoradapter madapter;

// If non-null, this is the current filter the user has provided.
String mcurfilter;

@ Override public void onactivitycreated (bundle savedinstancestate ){
Super. onactivitycreated (savedinstancestate );

// Give some text to display if there is no data. In a real
// Application this wocould come from a resource.
Setemptytext ("No phone numbers ");

// We have a menu item to show in action bar.
Sethasoptionsmenu (true );

// Create an empty adapter we will use 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 the loader. either re-connect with an existing one,
// Or start a new one.
Getloadermanager (). initloader (0, null, this );
}

@ Override public void oncreateoptionsmenu (menu, menuinflater Inflater ){
// Place an action bar item for searching.
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 action bar search text has changed. Update
// The search filter, and restart the loader to do a new Query
// With this filter.
Mcurfilter =! Textutils. isempty (newtext )? Newtext: NULL;
Getloadermanager (). restartloader (0, null, this );
Return true;
}

@ Override public Boolean onquerytextsubmit (string query ){
// Don't care about this.
Return true;
}

@ Override public void onlistitemclick (listview L, view V, int position, long ID ){
// Insert desired behavior here.
Log. I ("fragmentcomplexlist", "item clicked:" + id );
}

// These are the contacts rows that we will retrieve.
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 ){
// This is called when a new loader needs to be created. This
// Sample only has one loader, so we don't 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
// 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 ");
}

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 );
}

Public void onloaderreset (loader <cursor> loader ){
// This is called when the last cursor provided to onloadfinished ()
// Above is about to be closed. We need to make sure we are no
// Longer using it.
Madapter. swapcursor (null );
}

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.