Android学习笔记_57_ExpandableListView控件应用
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控件应用的更多相关文章
- [Android学习笔记]组合控件的使用
组合控件的使用 开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需关心内部逻辑,只需获取处理后 ...
- Android学习笔记_11_ListView控件使用
一.界面设计: 1.activity_main.xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk ...
- android学习笔记七——控件(DatePicker、TimePicker、ProgressBar)
DatePicker.TimePicker ==> DatePicker,用于选择日期 TimePicker,用于选择时间 两者均派生与FrameLayout,两者在FrameLayout的基础 ...
- 十三、Android学习笔记_Andorid控件样式汇总
<!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...
- Android学习笔记_75_Andorid控件样式汇总
<!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...
- android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件
主要参考<第一行代码> 1.TextView: 功能与传统的桌面应用开发中的Label控件相似,用于显示文本信息 如: <TextView android:layout_width= ...
- android菜鸟学习笔记14----Android控件(三) ListView的简单使用
MVC模式: MVC的基本原理就是通过Controller连接View和Model.当View中所显示的数据发生变化时,会通知Controller,然后由Controller调用Model中的相关方法 ...
- android菜鸟学习笔记13----Android控件(二) 自定义控件简单示例
有时候,可能觉得系统提供的控件太丑,就会需要自定义控件来实现自己想要的效果. 以下主要参考<第一行代码> 1.自定义一个标题栏: 系统自带的标题栏很丑,且没什么大的作用,所以我们之前会在o ...
- iOS学习笔记——基础控件(上)
本篇简单罗列一下一些常用的UI控件以及它们特有的属性,事件等等.由于是笔记,相比起来不会太详细 UIView 所有UI控件都继承于这个UIView,它所拥有的属性必是所有控件都拥有,这些属性都是控件最 ...
随机推荐
- DBNull.Value.ToString() == string.Empty
Console.WriteLine(DBNull.Value.ToString() == string.Empty); //True
- Notepad++的ftp远程编辑功能
我们主要来说说NppFTP的使用方法: 1.启动notepad++后,点击插件-->NppFTP-->Show NppFTP Window,就可以显示NppFTP的管理窗口了. 2.在Np ...
- windows环境下MySQL-5.7.12-winx64下载安装与配置
系统:64位Win-7 官网压缩包:mysql-5.7.12-winx64.zip 前后花了一些时间,以前都是下载软件直接安装在本地,现在这个不一样,下载压缩包后要解压缩到安装目录,然后在控制台下配置 ...
- SpringBoot | 第三十三章:Spring web Servcies集成和使用
前言 最近有个单位内网系统需要对接统一门户,进行单点登录和待办事项对接功能.一般上政府系统都会要求做统一登录功能,这个没啥问题,反正业务系统都是做单点登录的,改下shiro相关类就好了.看了接入方案, ...
- 图像文字识别(OCR)用什么算法小结
说明:主要考虑深度学习的方法,传统的方法不在考虑范围之内. 1.文字识别步骤 1.1detection:找到有文字的区域(proposal). 1.2classification:识别区域中的文字. ...
- 操蛋的Django model------select_related() 主要用于一对一和一对多
实例: 创建表,表都是一对一,一对多 class Province(models.Model): name = models.CharField(max_length=10) class City(m ...
- 《Python编程从入门到实践》_第十章_文件和异常
读取整个文件 文件pi_digits.txt #文件pi_digits.txt 3.1415926535 8979323846 2643383279 下面的程序打开并读取整个文件,再将其内容显示到屏幕 ...
- 【Shell】shell 判断文件夹或文件是否存在
1.文件夹不存在则创建,文件夹是directory if [ ! -d "/data/" ];then mkdir /data else echo "文件夹已经存在&qu ...
- fullpage的使用以及less, Sass的属性和JQuery自定义插件的声明和使用
使用fullpage的步骤 1 导入JQuery.js fullpage,js fullpage.css 2 组建网页布局,最外层id="fullpage" 单页class=& ...
- win10的xbox下载应用或者游戏时,出现0x80070422和0x80073D0A的解决办法
这个错误:0x80070422是因为关闭了windows update这个服务导致的 这个错误:0x80073D0A是因为关闭了windows firewall这个服务导致的 具体操作: cmd下se ...