SimpleExpandableListActivity为ExpandableListActivity传递数据
ExpandableListActivity为ListActivity传递数据
先看布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:drawSelectorOnTop="false"/>
<TextView
android:text="No Data"
android:id="@id/android:empty"
android:layout_width="fill_parent"/>
</LinearLayout>
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
android:text="No Data" android:id="@+id/textView_group" android:layout_height="fill_parent" android:layout_width="fill_parent" android:paddingLeft="60dp" android:paddingTop="10dp" android:textSize="25dp">
</TextView>
</LinearLayout>
<TextView android:text="No Data" android:layout_width="fill_parent"
android:id="@+id/textView_child" android:layout_height="fill_parent" android:paddingLeft="40dp" android:paddingTop="15dp" android:textSize="20dp" android:paddingBottom="5dp"></TextView>
以上是各个级别的布局文件
package com.ExpandableListActivityDemo;
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 MainActivity extends ExpandableListActivity {
/** ExpandableListActivity列表嵌套列表---->MainActivity继承了ExpandableListActivity */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 数据的准备构成
// 列表分2大类
//定义一个list,该list对象为一级条目提供数据,次代码为两个list条目
List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
Map<String, String> group1 = new HashMap<String, String>();
group1.put("group", "group1");
groups.add(group1);
Map<String, String> group2 = new HashMap<String, String>();
group2.put("group", "group2");
groups.add(group2);
// 每类有2组数据
//定义一个list,该list对象为第一个一级条目提供二级条目数据,次代码为两个list条目
List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
Map<String, String> child1_1 = new HashMap<String, String>();
child1_1.put("child", "child1");
child1.add(child1_1);
Map<String, String> child1_2 = new HashMap<String, String>();
child1_2.put("child", "child2");
child1.add(child1_2);
// 定义一个list,该list对象为第二个一级条目提供二级条目数据
List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
Map<String, String> child2_1 = new HashMap<String, String>();
child2_1.put("child", "child1");
Map<String, String> child2_2 = new HashMap<String, String>();
child2_2.put("child", "child2");
child2.add(child2_1);
child2.add(child2_2);
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
childs.add(child1);
childs.add(child2);
// SimpleExpandableListAdapter向ExpandableListActivity推送数据
//new String[] { "group" },new int[] { R.id.textView_group }这两个参数为一级条目的键值对
SimpleExpandableListAdapter simpleExpandableListAdapter = new SimpleExpandableListAdapter(
this, groups, R.layout.group, new String[] { "group" },
new int[] { R.id.textView_group }, childs, R.layout.child,
new String[] { "child" }, new int[] { R.id.textView_child });
setListAdapter(simpleExpandableListAdapter);
}
}