第一步:写布局文件
思路:
继承ExpandableListActivity,并且contentView必须包含ExpandableListView(id必须为@android:id/list),通过SimpleExpandableListAdapter适配器来绑定数据
代码:
1.main.xml(主页面)
<?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="vertical" >
<ExpandableListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ExpandableListView>
</LinearLayout>
2.group.xml(一级目录布局文件)
<?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="vertical" >
<TextView
android:id="@+id/groupid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
3.child.xml(二级目录布局文件)
<?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="vertical" >
<TextView
android:id="@+id/groupid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
第二步、写Activtiy
package com.geek.elv;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
public class MainActivity extends Activity {
ExpandableListView elv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//到数据库的groups表里面查询一级目录
SQLiteDatabase db=this.openOrCreateDatabase("common.db", MODE_APPEND, null);
Cursor cursor=db.query("groups", null, null, null, null, null, null);
//定义一个List,该List对象为一级条目提供数据
List<Map<String, Object>> grouplist =new ArrayList<Map<String,Object>>();
//存一级目录的数据
Map<String, Object> groupmap=null;
//定义一个List,该List对象用来存储所有的二级条目的数据
List<List<Map<String, Object>>> childs=new ArrayList<List<Map<String,Object>>>();
//定义一个List,该List对象为第一个一级条目提供二级条目的数据
List<Map<String, Object>> childlist1=null;
//存二级目录数据
Map<String, Object> childmap1=null;
//初始化控件
init();
//循环查询出来的一级目录
while (cursor.moveToNext()) {
//new 一个存一级目录的map集合
groupmap=new HashMap<String, Object>();
//把查出来的一级目录添加到map集合里面
groupmap.put("group",cursor.getString(cursor.getColumnIndex("name")) );
//把一级map集合添加到存一级目录的lis<map<string,object>集合里面
grouplist.add(groupmap);
//根据groups表里查出的一级目录ID来查询二级目录
Cursor cursor2=db.query("groupitem", null,"groupid=?" , new String[]{cursor.getString(cursor.getColumnIndex("idx"))}, null, null, null);
//new 一个lis<map<string,object>集合 该list为该一级目录提供二级目录数据
childlist1=new ArrayList<Map<String,Object>>();
//根据groups表里查出的一级目录ID来循环查询二级目录
while (cursor2.moveToNext()) {
//new 一个存二级目录数据的map集合
childmap1=new HashMap<String, Object>();
//把查出来的二级目录数据添加打map集合了
childmap1.put("child", cursor2.getString(cursor2.getColumnIndex("item")));
//把存放二级目录的map集合添加到存放二级目录的lis<map<string,object>集合里面
childlist1.add(childmap1);
}
//把存放二级目录的lis<map<string,object>集合添加到 List<List<Map<String, Object>>>集合里面
childs.add(childlist1);
//关闭游标
cursor2.close();
}
//生成一个SimpleExpandableListAdapter对象
SimpleExpandableListAdapter sea=new SimpleExpandableListAdapter
(this, //上下文
grouplist, //一级条目的数据
R.layout.group, //设置一级条目没展开的样式的布局文件
R.layout.group, //设置一级目录展开的样式的布局文件
new String[]{"group"}, //指定一级条目数据的
new int[]{R.id.groupid}, //指定一级目录数据显示控件的id
childs, //指定二级条目的数据
R.layout.child, //用来设置二级条目样式的布局文件
new String[]{"child"}, //指定二级目录数据的
new int[]{R.id.childid}); //指定二级条目数据显示控件的id
//把适配器绑定到ExpandableListView控件里面
elv.setAdapter(sea);
//关闭游标
cursor.close();
}
private void init() {
elv = (ExpandableListView) findViewById(android.R.id.list);
}
}
思路:
继承ExpandableListActivity,并且contentView必须包含ExpandableListView(id必须为@android:id/list),通过SimpleExpandableListAdapter适配器来绑定数据
代码:
1.main.xml(主页面)
<?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="vertical" >
<ExpandableListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ExpandableListView>
</LinearLayout>
2.group.xml(一级目录布局文件)
<?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="vertical" >
<TextView
android:id="@+id/groupid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
3.child.xml(二级目录布局文件)
<?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="vertical" >
<TextView
android:id="@+id/groupid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
第二步、写Activtiy
package com.geek.elv;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
public class MainActivity extends Activity {
ExpandableListView elv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//到数据库的groups表里面查询一级目录
SQLiteDatabase db=this.openOrCreateDatabase("common.db", MODE_APPEND, null);
Cursor cursor=db.query("groups", null, null, null, null, null, null);
//定义一个List,该List对象为一级条目提供数据
List<Map<String, Object>> grouplist =new ArrayList<Map<String,Object>>();
//存一级目录的数据
Map<String, Object> groupmap=null;
//定义一个List,该List对象用来存储所有的二级条目的数据
List<List<Map<String, Object>>> childs=new ArrayList<List<Map<String,Object>>>();
//定义一个List,该List对象为第一个一级条目提供二级条目的数据
List<Map<String, Object>> childlist1=null;
//存二级目录数据
Map<String, Object> childmap1=null;
//初始化控件
init();
//循环查询出来的一级目录
while (cursor.moveToNext()) {
//new 一个存一级目录的map集合
groupmap=new HashMap<String, Object>();
//把查出来的一级目录添加到map集合里面
groupmap.put("group",cursor.getString(cursor.getColumnIndex("name")) );
//把一级map集合添加到存一级目录的lis<map<string,object>集合里面
grouplist.add(groupmap);
//根据groups表里查出的一级目录ID来查询二级目录
Cursor cursor2=db.query("groupitem", null,"groupid=?" , new String[]{cursor.getString(cursor.getColumnIndex("idx"))}, null, null, null);
//new 一个lis<map<string,object>集合 该list为该一级目录提供二级目录数据
childlist1=new ArrayList<Map<String,Object>>();
//根据groups表里查出的一级目录ID来循环查询二级目录
while (cursor2.moveToNext()) {
//new 一个存二级目录数据的map集合
childmap1=new HashMap<String, Object>();
//把查出来的二级目录数据添加打map集合了
childmap1.put("child", cursor2.getString(cursor2.getColumnIndex("item")));
//把存放二级目录的map集合添加到存放二级目录的lis<map<string,object>集合里面
childlist1.add(childmap1);
}
//把存放二级目录的lis<map<string,object>集合添加到 List<List<Map<String, Object>>>集合里面
childs.add(childlist1);
//关闭游标
cursor2.close();
}
//生成一个SimpleExpandableListAdapter对象
SimpleExpandableListAdapter sea=new SimpleExpandableListAdapter
(this, //上下文
grouplist, //一级条目的数据
R.layout.group, //设置一级条目没展开的样式的布局文件
R.layout.group, //设置一级目录展开的样式的布局文件
new String[]{"group"}, //指定一级条目数据的
new int[]{R.id.groupid}, //指定一级目录数据显示控件的id
childs, //指定二级条目的数据
R.layout.child, //用来设置二级条目样式的布局文件
new String[]{"child"}, //指定二级目录数据的
new int[]{R.id.childid}); //指定二级条目数据显示控件的id
//把适配器绑定到ExpandableListView控件里面
elv.setAdapter(sea);
//关闭游标
cursor.close();
}
private void init() {
elv = (ExpandableListView) findViewById(android.R.id.list);
}
}