利用SimpleExpandableListAdapter为ExpandableListActivity提供数据

  首先MainActivity继承自ExpandableListActivity,其中的声明如下:

    setContentView(R.layout.expandmain);
    //定义一个:List,该List对象为一级条目提供数据
    List<Map<String,String>> parents = new ArrayList<Map<String,String>>();
    Map<String,String> parent1 = new HashMap<String,String>();
    parent1.put("group", "parent1");
    Map<String,String> parent2 = new HashMap<String,String>();
    parent2.put("group", "parent2");
    parents.add(parent1);
    parents.add(parent2);
    //定义一个List,该List对象为第一个一级条目提供数据
    List<Map<String,String>> child1 = new ArrayList<Map<String,String>>();
    Map<String,String> childData1 = new HashMap<String,String>();
    childData1.put("child", "child1Data1");
    Map<String,String> childData2 = new HashMap<String,String>();
    childData2.put("child", "child1Data2");
    child1.add(childData1);
    child1.add(childData2);
    //定义一个List,该List对象为第二个一级条目提供数据
    List<Map<String,String>> child2 = new ArrayList<Map<String,String>>();
    Map<String,String> childData3 = new HashMap<String,String>();
    childData3.put("child", "child1Data3");
    Map<String,String> childData4 = new HashMap<String,String>();
    childData4.put("child", "child1Data4");
    child2.add(childData3);
    child2.add(childData4);
    //生成一个List,该List对象用来存储所有的二级条目的数据
    List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>();
    childs.add(child1);
    childs.add(child2);
    //context
    //一级条目的数据
    //用来设置一级条目样式的布局文件
    //指定一级条目数据的key
    //指定一级条目显示控件的ID
    //指定二级条目的数据
    //用来设置二级条目的布局文件
    //指定二级条目数据的key
    //指定二级条目数据显示控件的ID
    SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(this, parents, R.layout.parent, new String[]{"group"}, new int[]                           {R.id.parentTo}, childs, R.layout.child, new String[]{"child"},new int[]{R.id.childTo});
    //将SimpleExpandableListAdapter设置给当前的ExpandableListActivity
    setListAdapter(sela);

  其中expandmain.xml文件中的内容

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >
      <ExpandableListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"/>
    </LinearLayout>

  parent.xml文件的内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >
      <TextView android:id="@+id/parentTo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20px"
        android:textSize="26sp"
        android:text="No data"/>
    </LinearLayout>

  child.xml文件中的内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >
      <TextView
        android:id="@+id/childTo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20px"
        android:textSize="20sp"
        android:text="No data" />
    </LinearLayout>

posted @ 2015-08-13 22:31  best.lei  阅读(317)  评论(0编辑  收藏  举报