1、布局文件:

<?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/elv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ExpandableListView>
</LinearLayout>

2、填充数据:与listview使用比较像

package cn.itcast.mobilesafe.ui;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import cn.itcast.mobilesafe.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView; public class CommonNumActivity extends Activity {
private ExpandableListView elv;
private BaseExpandableListAdapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.common_num_query);
elv = (ExpandableListView) this.findViewById(R.id.elv); // 判断这个commonnum.db的数据库是否被放置到了sd卡上
// 如果不在sd卡上 要把db从asset目录拷贝到数据库
File file = new File("/sdcard/commonnum.db");
if (!file.exists()) {
copyfile();
} // listview 是怎么设置数据的?
// lv.setAdapter(); ->BaseAdapter
// elv.setAdapter ExpendAdapter ->BaseExpendAdapter elv.setAdapter(new MyAdapter());
} private class MyAdapter extends BaseExpandableListAdapter { // 返回有多少个分组
public int getGroupCount() {
int count=0;
SQLiteDatabase db = SQLiteDatabase.openDatabase("/sdcard/commonnum.db", null, SQLiteDatabase.OPEN_READONLY);
if(db.isOpen()){
Cursor cursor = db.rawQuery("select count(*) from classlist", null);
if(cursor.moveToFirst()){
count = cursor.getInt(0);
}
cursor.close();
db.close();
}
return count;
} // 返回某个分组对应的子孩子的条目个数 public int getChildrenCount(int groupPosition) { int count=0;
int tableindex = groupPosition+1;
String sql = "select count(*) from table"+tableindex; SQLiteDatabase db = SQLiteDatabase.openDatabase("/sdcard/commonnum.db", null, SQLiteDatabase.OPEN_READONLY);
if(db.isOpen()){
Cursor cursor = db.rawQuery(sql, null);
if(cursor.moveToFirst()){
count = cursor.getInt(0);
}
cursor.close();
db.close();
}
return count; } // 返回当前groupPosition 对应位置的对象
public Object getGroup(int groupPosition) {
return null;
} // 返回groupPosition第childPosition个子孩子对应的条目
public Object getChild(int groupPosition, int childPosition) {
return null;
} // 获取分组的id
public long getGroupId(int groupPosition) {
return groupPosition;
} // 获取分组中子孩子id
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
// 是否允许子孩子有点击事件,默认子孩子没有点击事件
public boolean hasStableIds() {
return false;
} //获取组视图
@SuppressLint("SdCardPath")
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView tv = new TextView(CommonNumActivity.this);
String text ="";
int currentpos = groupPosition+1;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)){
SQLiteDatabase db = SQLiteDatabase.openDatabase("/sdcard/commonnum.db", null, SQLiteDatabase.OPEN_READONLY);
if(db.isOpen()){
Cursor cursor = db.rawQuery("select name from classlist where idx=?", new String[]{currentpos+""});
if(cursor.moveToFirst()){
text = cursor.getString(0);
}
cursor.close();
db.close();
}
tv.setText(" "+text);
}
return tv;
} //获取孩子视图
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView tv = new TextView(CommonNumActivity.this);
StringBuilder sb = new StringBuilder();
int tableindex = groupPosition+1;
int childindex = childPosition+1;
String sql = "select number,name from table"+tableindex; SQLiteDatabase db = SQLiteDatabase.openDatabase("/sdcard/commonnum.db", null, SQLiteDatabase.OPEN_READONLY);
if(db.isOpen()){
Cursor cursor = db.rawQuery(sql+ " where _id=?", new String[]{childindex+""});
if(cursor.moveToFirst()){
sb.append( cursor.getString(0)); //number
sb.append(":");
sb.append( cursor.getString(1)); //name }
cursor.close();
db.close();
}
String text = sb.toString();
tv.setText(text);
return tv;
} public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
} }
private void copyfile() {
AssetManager manager = getAssets();
try {
InputStream is = manager.open("commonnum.db");
File file = new File("/sdcard/commonnum.db");
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.flush();
fos.close(); } catch (IOException e) {
e.printStackTrace();
}
} }

3、如何优化:

  A:考虑重用convertView

  B:不用关闭数据库,

Android学习笔记_57_ExpandableListView控件应用的更多相关文章

  1. [Android学习笔记]组合控件的使用

    组合控件的使用 开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需关心内部逻辑,只需获取处理后 ...

  2. Android学习笔记_11_ListView控件使用

    一.界面设计: 1.activity_main.xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk ...

  3. android学习笔记七——控件(DatePicker、TimePicker、ProgressBar)

    DatePicker.TimePicker ==> DatePicker,用于选择日期 TimePicker,用于选择时间 两者均派生与FrameLayout,两者在FrameLayout的基础 ...

  4. 十三、Android学习笔记_Andorid控件样式汇总

    <!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...

  5. Android学习笔记_75_Andorid控件样式汇总

    <!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...

  6. android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件

    主要参考<第一行代码> 1.TextView: 功能与传统的桌面应用开发中的Label控件相似,用于显示文本信息 如: <TextView android:layout_width= ...

  7. android菜鸟学习笔记14----Android控件(三) ListView的简单使用

    MVC模式: MVC的基本原理就是通过Controller连接View和Model.当View中所显示的数据发生变化时,会通知Controller,然后由Controller调用Model中的相关方法 ...

  8. android菜鸟学习笔记13----Android控件(二) 自定义控件简单示例

    有时候,可能觉得系统提供的控件太丑,就会需要自定义控件来实现自己想要的效果. 以下主要参考<第一行代码> 1.自定义一个标题栏: 系统自带的标题栏很丑,且没什么大的作用,所以我们之前会在o ...

  9. iOS学习笔记——基础控件(上)

    本篇简单罗列一下一些常用的UI控件以及它们特有的属性,事件等等.由于是笔记,相比起来不会太详细 UIView 所有UI控件都继承于这个UIView,它所拥有的属性必是所有控件都拥有,这些属性都是控件最 ...

随机推荐

  1. 【程序员技术练级】学习一门脚本语言 python(三)跟数据库打交道

    接着上一篇,该篇讲述使用python对数据库进行基本的CRUD操作,这边以sqlite3为例子,进行说明.sqlite3 是一个非常轻型的数据库,安装和使用它是非常简单的,这边就不进行讲述了. 在py ...

  2. git stash压栈

    git stash 用于暂存当前正在进行的工作,如想pull最新的代码,又不想加新的commit,或者为了fix一个紧急的bug,先stash,返回到自己上一个commit. 修改完bug后,再执行g ...

  3. 集合之Iterator迭代器

      Iterator迭代器概述: java中提供了很多个集合,它们在存储元素时,采用的存储方式不同.我们要取出这些集合中的元素,可通过一种通用的获取方式来完成. Collection集合元素的通用获取 ...

  4. JavaScirpt(JS)——BOM浏览器对象模型

    一.BOM概念 BOM(Browser Object Model)即浏览器对象模型.可以对浏览器窗口进行访问和操作.使用 BOM,开发者可以移动窗口.改变状态栏中的文本以及执行其他与页面内容不直接相关 ...

  5. SQLAlchemy的使用---外键ForeignKey数据增删改查

    # 添加数据 from sqlalchemy.orm import sessionmaker from create_table_ForeignKey import engine, Student, ...

  6. PHP正则表达式实例汇总

    $str = preg_replace("/(<a.*?>)(.*?)(<\/a>)/", '\1<span class="link&quo ...

  7. CSS文字有关属性

    font-size|family|weight|style 大小字体加粗斜体 color|opacity 颜色透明度 height+line-height:垂直居中 overflow:hidden|v ...

  8. SharePoint 2013 - User Custom Action

    1. User Custom Action包含Ribbon和ECB,以及Site Action菜单等,参考此处: 2. 系统默认ECB的Class为: ms-core-menu-box --> ...

  9. 关联关系的CRUD

    关联关系中的CRUD_Cascade_Fetch 1. hibernate_1700_one2many_many2one_bi_crud 2. 设定 cascade 可以设定在持久化时对于关联对象的操 ...

  10. Spark天堂之门解密

    本课主题 什么是 Spark 的天堂之门 Spark 天堂之门到底在那里 Spark 天堂之门源码鉴赏 引言 Spark 天堂之门就是SparkContext,这篇文章会从 SparkContext ...