Getting started with Android: Use the expandablelistview Control

Source: Internet
Author: User

This article describes how to use the expandablelistview control and how to use the expandablelistactivity class and simpleexpandablelistadapter adapter.

The code is described as follows:

First, layout contains three layout files:

Main. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Expandablelistview
Android: Id = "@ ID/Android: List"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: drawselectid Top = "true"
/> <! -- Android: drawselectid top: whether to highlight -->
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "no data"
Android: Id = "@ ID/Android: empty"
/>
</Linearlayout>

---------------------------------------------------------------------------

Group. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "no data"
Android: Id = "@ + ID/tvgroup"
Android: paddingbottom = "10px"
Android: paddingleft = "50px"

/>
</Linearlayout>

--------------------------------------------------

Child. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
 
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "no data"
Android: Id = "@ + ID/tvchild"
Android: paddingleft = "50px"
/>
</Linearlayout>

---------------------------------------------------------------------

Java code:

Import java. util. arraylist;
Import java. util. hashmap;
Import java. util. List;
Import java. util. Map;

Import Android. App. expandablelistactivity;
Import Android. OS. Bundle;
Import Android. widget. simpleexpandablelistadapter;

Public class expandablelistactivitytest extends expandablelistactivity {
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

List <Map <string, string> groups = new arraylist <Map <string, string> ();
Map <string, string> group1 = new hashmap <string, string> ();
Group1.put ("group", "huangweiyong ");

Map <string, string> group2 = new hashmap <string, string> ();
Group2.put ("group", "Xiaoqi ");

Map <string, string> Group3 = new hashmap <string, string> ();
Group3.put ("group", "HAHAHA ");

Groups. Add (group1 );
Groups. Add (group2 );
Groups. Add (Group3 );

List <Map <string, string> Child1 = new arraylist <Map <string, string> ();
Map <string, string> child1data1 = new hashmap <string, string> ();
Child1data1. Put ("child", "child1data1 ");
Map <string, string> child1data2 = new hashmap <string, string> ();
Child1data2. Put ("child", "child1data2 ");
Child1.add (child1data1 );
Child1.add (child1data2 );

List <Map <string, string> child2 = new arraylist <Map <string, string> ();
Map <string, string> child2data1 = new hashmap <string, string> ();
Child2data1. Put ("child", "child1data1 ");
Child2.add (child2data1 );

List <Map <string, string> Child3 = new arraylist <Map <string, string> ();
Map <string, string> child3data1 = new hashmap <string, string> ();
Child3data1. Put ("child", "child1data1 ");
Child3.add (child3data1 );

List <list <Map <string, string >>> Childs = new arraylist <list <Map <string, string >>> ();
Childs. Add (Child1 );
Childs. Add (child2 );
Childs. Add (Child3 );

/**
* Generate a simpleexpandablelistadapter object
* 1. Context
* 2. Data of level 1 entries
* 3. Set the layout file for the first-level entry Style
* 4. Specify the key of the first-level entry data
* 5. ID of the control displayed for the first-level entry
* 6. specify data of level 2 entries
* 7. layout file used to set the second-level entry Style
* 8. Specify the key of the second-level entry data
* 9. ID of the control displayed for the specified second-level entry data
*/
Simpleexpandablelistadapter SELA = new simpleexpandablelistadapter (
This, groups, R. layout. Group, New String [] {"group "},
New int [] {R. Id. tvgroup}, Childs, R. layout. Child,
New String [] {"child"}, new int [] {R. Id. tvchild });
// Set the adapter object to expandablelistactivity
Setlistadapter (SELA );
}
}

 

 

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.