第一步:写布局文件
思路:
继承ExpandableListActivity,并且contentView必须包含ExpandableListView(id必须为@android:id/list),通过SimpleExpandableListAdapter适配器来绑定数据
代码:
1.main.xml(主页面)
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
2.group.xml(一级目录布局文件)
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/groupid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
3.child.xml(二级目录布局文件)
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/groupid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
第二步、写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