Listactivity and viewgroup

Source: Internet
Author: User
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 of the copied Code uses the Android. R. layout. two_line_list_item of the system.

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

}

Class listitemview extends viewgroup {

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

}

}

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.