Android ApiDemos example resolution (113): Views-& gt; Expandable Lists-& am

Source: Internet
Author: User

The above class uses the custom ExpandableListAdapter to define the group and childern Of The Expandable list using arrays. It is the inheritance relationship between ExpandableListAdapter.

 

CursorTreeAdapter supports using a series of Cursors as the data source of ExpandableListView. A Cursor at the top is defined as an Expandable group. Other Cursor can define its sub-Item for a specific Group through getChildernCursor. All Cursor objects must contain a column named "_ id". Otherwise, this class cannot work.

For usage of Cursor, see Android ApiDemos example resolution (10): App-> Activity-> QuickContactsDemo.

The CursorTreeAdapter class name contains a "Tree". Unlike TreeView on other platforms, Expandable List supports only two layers.

ResourceCursorTreeAdapter adds two methods for CursorTreeAdapter: newChildView and newGroupView. The constructor allows you to specify XML layout resources for the group and child of ExpandableListView.

SimpleCursorTreeAdapter provides an easy way to map columns in a table to TextView or ImageView (defined in XML resources. You can specify which columns are needed and which View is used for each column to display.

In this example, the subclass MyExpandableListAdapter of SimpleCursorTreeAdapter uses the Contact address book as the data source.

The contact name in the address book is used as the Group, and all the phone numbers corresponding to each contact are used as the childeren of the group.

[Java]
Cursor groupCursor = managedQuery (People. CONTENT_URI,
New String [] {People. _ ID, People. NAME}, null );
 
// Cache the ID column index
MGroupIdColumnIndex = groupCursor. getColumnIndexOrThrow (People. _ ID );
 
// Set up our adapter
MAdapter = new MyExpandableListAdapter (groupCursor,
This,
Android. R. layout. simple_expandable_list_item_1,
Android. R. layout. simple_expandable_list_item_1,
New String [] {People. NAME}, // Name for group layouts
New int [] {android. R. id. text1 },
New String [] {People. NUMBER}, // Number for child layouts
New int [] {android. R. id. text1 });

Cursor groupCursor = managedQuery (People. CONTENT_URI,
New String [] {People. _ ID, People. NAME}, null );

// Cache the ID column index
MGroupIdColumnIndex = groupCursor. getColumnIndexOrThrow (People. _ ID );

// Set up our adapter
MAdapter = new MyExpandableListAdapter (groupCursor,
This,
Android. R. layout. simple_expandable_list_item_1,
Android. R. layout. simple_expandable_list_item_1,
New String [] {People. NAME}, // Name for group layouts
New int [] {android. R. id. text1 },
New String [] {People. NUMBER}, // Number for child layouts
New int [] {android. R. id. text1 });


Its constructor is defined as follows:

MyExpandableListAdapter (Cursor cursor, Context context, int groupLayout,
Int childLayout, String [] groupFrom, int [] groupTo, String [] childrenFrom,
Int [] childrenTo ){

Cursor: group Cursor. In this example, it is the contact name. You can see that the column of this Cursor must have a column named _ ID.
Context: context object. You can use activity.
GroupLayout: Group Layout resource ID. In this example, the system resource simple_expandable_list_item_1 is used.
ChildLayout: child Layout resource ID. In this example, the system resource simple_expandable_list_item_1 is used.
GroupFrom: array of group column names to be displayed. In this example, Name is used.
GroupTo: id of the View corresponding to the columns in each group. This example is a text.
ChildrenFrom: name of the group members to be displayed. In this example, the name is Number.
ChildrenTo: id of the View corresponding to the columns of each group member. This example is a text.



 

 

Author: mapdigit

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.