zoukankan      html  css  js  c++  java
  • 利用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>

  • 相关阅读:
    JdbcTemplate查询数据 三种callback之间的区别
    velocity加减运算注意格式 ,加减号的左右都要有空格
    java怎样读取数据库表中字段的数据类型?
    一台电脑同时运行多个tomcat配置方法
    启动PL/SQL Developer 报字符编码不一致错误 Database character set (AL32UTF8) and Client character set (ZHS16GBK) are different. Character set conversion may cause unexpected results. Note: you can set the client
    PL/SQL database character set(AL32UTF8) and Client character set(ZHS16GBK) are different 2012-04-11 13:01
    sqlserver得到昨天的数据
    iOS消息机制
    Narrow Art Gallery
    Hadoop入门进阶步步高(一)-环境准备
  • 原文地址:https://www.cnblogs.com/zhanglei93/p/4728633.html
Copyright © 2011-2022 走看看