Android Layout-List View

Source: Internet
Author: User

This article translated from: http://developer.android.com/guide/topics/ui/layout/listview.html

Listview is a view group used to display the scrolling project list. It adapts to an adapter to automatically Insert list items. The adapter pulls content from queries such as arrays or databases and converts the results of each project into a view that can be placed in the list.

For how to use an adapter to automatically insert a view, see use an adapter to create a layout.

Use the loader

To avoid blocking the main thread of your application during query, using cursorloader asynchronous tasks to query cursor is a standard method. When cursorloader receives the cursor result, loadercallbacks receives a callback for the onloadfinished () method. This is where you use the new cursor to update your adapter, and then the List View displays the result.

Although cursorloader APIs are introduced for the first time in android3.0 (API level 11), these APIs are also valid in the supported class libraries. Therefore, in devices that support running android1.6 or above, your application can also be used.

For information on using loader to asynchronously load data, see the loaders guide.

Example

The following example uses listactivity. By default, it is an activity that contains only one listview layout element. It queries the contacts provider to obtain the list of names and phone numbers.

To use cursorloader to automatically load data to the list view, this activity implements the loadercallbacks interface.

Public class listviewloader extends listactivity


Implements loadermanager. loadercallbacks <cursor> {


// This is the adapter being used to display the list's data


Simplecursoradapter madapter;


// These are the contacts rows that we will retrieve


Static final string [] projection = new string [] {contactscontract. Data. _ id,


Contactscontract. Data. display_name };


// This is the select criteria


Static final string selection = "(" +


Contactscontract. Data. display_name + "notnull) and (" +


Contactscontract. Data. display_name + "! = ''))";


@ Override


Protected void oncreate (bundle savedinstancestate ){


Super. oncreate (savedinstancestate );


// Create a progress bar to display while the list Loads


Progressbar = new progressbar (this );


Progressbar. setlayoutparams (New layoutparams (layoutparams. wrap_content,


Layoutparams. wrap_content, gravity. Center ));


Progressbar. setindeterminate (true );


Getlistview (). setemptyview (progressbar );


// Must add the progress bar to the root of the layout


Viewgroup root = (viewgroup) findviewbyid (Android. R. Id. content );


Root. addview (progressbar );


// For the cursor adapter, specify which Columns go into which views


String [] fromcolumns = {contactscontract. Data. display_name };


Int [] toviews = {Android. R. Id. text1}; // The textview in simple_list_item_1


// Create an empty adapter we will use to display the loaded data.


// We pass NULL for the cursor, then update it in onloadfinished ()


Madapter = new simplecursoradapter (this,


Android. R. layout. simple_list_item_1, null,


Fromcolumns, toviews, 0 );


Setlistadapter (madapter );


// Prepare the loader. either re-connect with an existing one,


// Or start a new one.


Getloadermanager (). initloader (0, null, this );


}


// Called when a new loader needs to be created


Public loader <cursor> oncreateloader (int id, bundle ARGs ){


// Now create and return a cursorloader that will take care


// Creating a cursor for the data being displayed.


Return new cursorloader (this, contactscontract. Data. content_uri,


Projection, selection, null, null );


}


// Called when a previusly created loader has finished loading


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


}


// Called when a previusly created loader is reset, making the data unavailable


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


}


@ Override


Public void onlistitemclick (listview L, view V, int position, long ID ){


// Do something when a list item is clicked


}

}

Note:In this example, You need to query the contacts provider. to execute this code, your application must apply for the read_contacts permission in the configuration file:

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

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.