Today, I carefully studied the quick contactsdemo example in the API demo. I feel that I have a better understanding of listactivity. The following is a summary of listactivity based on the official documents and your own research.
Screen Layout
The default layout of listactivity consists of a full screen list in the center of the screen. If you do not want to use the default layout, you can use the setcontentview () method in the oncreate () method to set your own layout.
If you specify your own custom layout, your layout must contain a listview with ID "@ ID/Android: List. If you specify a view with the ID "@ ID/Android: empty", the view will be displayed if no data is displayed in the listview, meanwhile, the listview will be hidden.
Example:
1. layout file, Res/layout/Main. xml.
XML Code
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: paddingleft = "8dp" Android: paddingright = "8dp"> <! -- In addition to listview and view with the ID @ ID/Android: empty, we can also add view --> <textview Android: Id = "@ + ID/Android: title "Android: layout_width =" match_parent "Android: layout_height =" wrap_content "Android: text =" The following is a list: "/> <! -- Listview with ID @ ID/Android: List is a custom list layout. If no, the system calls the default layout --> <listview Android: id = "@ ID/Android: List" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Background = "#00ff00" Android: layout_weight = "1" Android: drawselectid Top = "false"/> <! -- If the listview contains no data, the view with the ID @ ID/Android: Empty is displayed. --> <textview Android: Id = "@ ID/Android: empty" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: textcolor = "# ff0000" Android: text = "no data" Android: gravity = "center_vertical | center_horizontal"/> <! -- In addition to listview and view with the ID @ ID/Android: empty, we can also add view --> <textview Android: Id = "@ + ID/Android: title "Android: layout_width =" match_parent "Android: layout_height =" wrap_content "Android: text =" The following is a list: "/> </linearlayout>
2. the Java file corresponding to the activity, listactivitydemo. java.
Java code
Package com. xeedroid;
Import java. util. arraylist;
Import java. util. List;
Import Android. App. listactivity;
Import Android. OS. Bundle;
Import Android. widget. arrayadapter;
Public class listactivitydemo extends listactivity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
List items = filllist ();
Arrayadapter adapter = new arrayadapter (this, Android. R. layout. simple_list_item_1, items );
Setlistadapter (adapter );
}
Private list filllist (){
List items = new arraylist ();
Items. Add ("Monday ");
Items. Add ("Tuesday ");
Items. Add ("Wednesday ");
Items. Add ("Thursday ");
Items. Add ("Friday ");
Items. Add ("Saturday ");
Items. Add ("Sunday ");
// Items. Clear ();
Return items;
}
}
3. The running effect is as follows:
4. Release the comments of "items. Clear ();" in Java code. The data source bound to the listview does not have data. The running effect is as follows:
The running result shows that the listview with a green background is not displayed.
Row Layout
Android allows you to specify a layout for a separate row in the list. You only need to specify a layout resource in the listadapter object.
A listadapter constructor has a parameter to specify the layout resources of each row. In addition, it has two other parameters to specify which data domain is associated with the objects in the row layout resource. These two parameters are generally parallel arrays.
Android provides some standard layout Resources in the R. layout class. For example, simple_list_item_1, simple_list_item_2, and two_line_list_item.
See example 1 (using simplecursoradapter ):
1. Use the default layout.
2. the Java code corresponding to the activity is as follows.
Java code
Package com. xeedroid;
Import Android. App. listactivity;
Import Android. database. cursor;
Import Android. OS. Bundle;
Import Android. provider. contactscontract. contacts;
Import Android. widget. listadapter;
Import Android. widget. simplecursoradapter;
Public class listactivitydemo extends listactivity {
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Cursor mcursor = This. getcontentresolver (). Query (contacts. content_uri,
Null, null );
Startmanagingcursor (mcursor );
Listadapter adapter = new simplecursoradapter (this,
Android. R. layout. two_line_list_item, mcursor, new string [] {
Contacts. display_name, contacts. times_contacted}, new int [] {
Android. R. Id. text1, Android. R. Id. text2 });
Setlistadapter (adapter );
}
}
The customized row layout uses the system Android. R. layout. two_line_list_item.
3. The running effect is as follows:
See example 2 (Use resourcecursoradapter ):
1. Use the default screen layout.
2. the Java code quickcontactsdemo. Java (with annotations) corresponding to the activity is as follows:
Java code
Package com. example. Android. APIs. app;
Import com. example. Android. APIs. R;
Import Android. App. listactivity;
Import Android. content. context;
Import Android. database. chararraybuffer;
Import Android. database. cursor;
Import Android. OS. Bundle;
Import Android. provider. contactscontract. contacts;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. widget. quickcontactbadge;
Import Android. widget. resourcecursoradapter;
Import Android. widget. textview;
Public class quickcontactsdemo extends listactivity {
Static final string [] contacts_summary_projection = new string [] {
Contacts. _ id, // 0
Contacts. display_name, // 1
Contacts. starred, // 2
Contacts. times_contacted, // 3
Contacts. contact_presence, // 4
Contacts. photo_id, // 5
Contacts. lookup_key, // 6
Contacts. has_phone_number, // 7
};
Static final int summary_id_column_index = 0;
Static final int summary_name_column_index = 1;
Static final int summary_starred_column_index = 2;
Static final int summary_times_contacted_column_index = 3;
Static final int summary_presence_status_column_index = 4;
Static final int summary_photo_id_column_index = 5;
Static final int summary_lookup_key = 6;
Static final int summary_has_phone_column_index = 7;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Search for all qualified contacts
String select = "(" + contacts. display_name + "notnull) and ("
+ Contacts. has_phone_number + "= 1) and ("
+ Contacts. display_name + "! = ''))";
Cursor c =
Getcontentresolver (). Query (contacts. content_uri, contacts_summary_projection, select,
Null, contacts. display_name + "collate localized ASC ");
// Send cursor to activity management
Startmanagingcursor (C );
// Create an adapter and bind the custom UI and data to be displayed with the adapter
Contactlistitemadapter adapter = new contactlistitemadapter (this, R. layout. quick_contacts, C );
// Bind the adapter to the current list Activity
Setlistadapter (adapter );
}
/*
* Resourcecursoradapter transmits data to the listactivity according to its requirements. Its ancestor class implements the list adapter interface.
* During the rendering of the listactivity interface, the newview and BindView methods are called for each record in the cursor to generate the UI in sequence.
*/
Private final class contactlistitemadapter extends resourcecursoradapter {
Public contactlistitemadapter (context, int layout, cursor c ){
Super (context, layout, C );
}
// Bind the view generated by newview to the data specified by the current cursor.
@ Override
Public void BindView (view, context, cursor ){
Final contactlistitemcache cache = (contactlistitemcache) view. gettag ();
Textview nameview = cache. nameview;
Quickcontactbadge photoview = cache. photoview;
// Set the name
Cursor. copystringtobuffer (summary_name_column_index, cache. namebuffer );
Int size = cache. namebuffer. sizecopied;
Cache. nameview. settext (Cache. namebuffer. Data, 0, size );
Final long contactid = cursor. getlong (summary_id_column_index );
Final string lookupkey = cursor. getstring (summary_lookup_key );
Cache. photoview. assigncontacturi (contacts. getlookupuri (contactid, lookupkey ));
}
// Generate view based on layout in contactlistitemadapter (context, int layout, cursor C)
@ Override
Public View newview (context, cursor, viewgroup parent ){
View view = super. newview (context, cursor, parent );
Contactlistitemcache cache = new contactlistitemcache ();
Cache. nameview = (textview) view. findviewbyid (R. Id. Name );
Cache. photoview = (quickcontactbadge) view. findviewbyid (R. Id. Badge );
// Tag is used to pass any object and expose the child view in the view generated by the current method as a parameter for BindView () to call
View. settag (cache );
Return view;
}
}
/*
* Custom Data Structure, used to store elements (each sub-view) in the view generated by newview)
*/
Final Static class contactlistitemcache {
Public textview nameview;
Public quickcontactbadge photoview;
Public chararraybuffer namebuffer = new chararraybuffer (128 );
}
}
3. The resource file corresponding to the row layout, Res/layout/quick_contacts.xml.
XML Code
<? XML version = "1.0" encoding = "UTF-8"?>
<Relativelayout
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: paddingleft = "0dip"
Android: paddingright = "9dip"
Android: layout_height = "wrap_content"
Android: minheight = "48dip">
<Quickcontactbadge
Android: Id = "@ + ID/badge"
Android: layout_marginleft = "2dip"
Android: layout_marginright = "14dip"
Android: layout_margintop = "4dip"
Android: layout_marginbottom = "3dip"
Android: layout_alignparentleft = "true"
Android: layout_alignparenttop = "true"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: src = "@ drawable/ic_contact_picture"
Style = "? Android: ATTR/quickcontactbadgestylewindowsmall "/>
<Textview
Android: Id = "@ + ID/name"
Android: textappearance = "? Android: ATTR/textappearancemedium"
Android: paddingleft = "2dip"
Android: layout_centervertical = "true"
Android: layout_torightof = "@ ID/badge"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"/>
</Relativelayout>
Listactivity gradually renders the entire UI Based on the layout file and data corresponding to the row.
4. The running effect is as follows:
Binding to data
We pass data to listview by implementing the listadapter class object.
The main classes used are simpleadapter, arrayadapter, simplecursoradapter, and resourcecursoradapter ).
Simplecursoradapter and resourcecursoradapter (abstract class) are subclasses of cursoradapter. There are two methods in cursoradapter: newview () and BindView (). newview is used to create a rowlayout, and BindView is used to bind data to the new rowlayout. Resourcecursoradapter is required because some data cannot be bound with simplecursoradapter.