This example describes how to use Map-based data as the data source of the Expandable List.
Group and Child are defined as follows:
[Java]
List <Map <String, String> groupData
= New ArrayList <Map <String, String> ();
List <Map <String, String> childData
= New ArrayList <List <Map <String, String >>> ();
List <Map <String, String> groupData
= New ArrayList <Map <String, String> ();
List <Map <String, String> childData
= New ArrayList <List <Map <String, String >>> ();
It looks a bit complicated :-). Map <String, String> is similar to the Hash table. It can be regarded as a row of the database table, with the column name as the keyword. For example, the data of a row in this example is
In this example, 20 sub-elements are defined for groupData, from group 0 to group 19. For each group, 15 sub-elements are defined, from child 0 to child 14.
Each element also has the following values:
With groupdata and childdata, you can call the SimpleExpandableListAdapter constructor:
[Java]
// Set up our adapter
MAdapter = new SimpleExpandableListAdapter (
This,
GroupData,
Android. R. layout. simple_expandable_list_item_1,
New String [] {NAME, IS_EVEN },
New int [] {android. R. id. text1, android. R. id. text2 },
ChildData,
Android. R. layout. simple_expandable_list_item_2,
New String [] {NAME, IS_EVEN },
New int [] {android. R. id. text1, android. R. id. text2}
);
// Set up our adapter
MAdapter = new SimpleExpandableListAdapter (
This,
GroupData,
Android. R. layout. simple_expandable_list_item_1,
New String [] {NAME, IS_EVEN },
New int [] {android. R. id. text1, android. R. id. text2 },
ChildData,
Android. R. layout. simple_expandable_list_item_2,
New String [] {NAME, IS_EVEN },
New int [] {android. R. id. text1, android. R. id. text2}
);
Public SimpleExpandableListAdapter (Context context,
List <? Extends Map <String,?> GroupData,
Int groupLayout, String [] groupFrom,
Int [] groupTo,
List <? Extends List <? Extends Map <String,? >>> ChildData,
Int childLayout,
String [] childFrom, int [] childTo)
Context: context object. You can use activity.
Groupdata: group data source
GroupLayout: Group Layout resource ID. In this example, the system resource simple_expandable_list_item_2 is used.
Childdata: child Data Source
ChildLayout: child Layout resource ID. In this example, the system resource simple_expandable_list_item_2 is used.
GroupFrom: array of group column names to be displayed
GroupTo: View id corresponding to the columns of each group
ChildrenFrom: name of the group members to be displayed
ChildrenTo: id of the View corresponding to the columns of each group member.
Simple_expandable_list_item_1.xml and simple_expandable_list_item_2.xml can be found in the http://www.netmite.com/android/mydroid/frameworks/base/core/res/res/layout/
Simple_expandable_list_item_1 defines a text box id as text1. In the original example, simple_expandable_list_item_1 is used to display the group. The corresponding IS_EVEN uses text2 to display the View id. Therefore, only Group xx is displayed. After the preceding modification, simple_expandable_list_item_2, NAME, and IS_EVEN are used for both group and child:
Author: mapdigit