package com.loaderman.expandablelistviewdemo;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList; /**
* <p>
* 常用号码数据库查询
*/ public class CommonNumberDao { //获取所有常用号码
public static ArrayList<GroupInfo> getCommonNumbers(Context ctx) { SQLiteDatabase database = SQLiteDatabase.openDatabase(ctx.getFilesDir()
.getAbsolutePath() + "/commonnum.db", null,
SQLiteDatabase.OPEN_READONLY); Cursor cursor = database.query("classlist", new String[]{"name", "idx"}, null, null,
null, null, null); ArrayList<GroupInfo> list = new ArrayList<>();
if (cursor != null) { while (cursor.moveToNext()) {
GroupInfo info = new GroupInfo(); info.name = cursor.getString(0);
info.idx = cursor.getString(1);
info.children = getChildrenList(info.idx, database);//获取当前组的孩子信息 list.add(info);
} cursor.close();
} database.close(); return list;
} //获取某组所有电话号码
private static ArrayList<ChildInfo> getChildrenList(String idx, SQLiteDatabase database) {
Cursor cursor = database.query("table" + idx, new String[]{"number", "name"}, null, null,
null, null, null); ArrayList<ChildInfo> list = new ArrayList<>();
if (cursor != null) { while (cursor.moveToNext()) {
ChildInfo info = new ChildInfo();
info.number = cursor.getString(0);
info.name = cursor.getString(1);
list.add(info);
} cursor.close();
} //database.ose();不能关闭数据库, 在getCommonNumbers中统一关闭数据库 return list;
} public static class GroupInfo {
public String name;
public String idx;
public ArrayList<ChildInfo> children;//当前组的所有电话号码
} public static class ChildInfo {
public String name;
public String number;
} }
package com.loaderman.expandablelistviewdemo;

import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private ArrayList<CommonNumberDao.GroupInfo> mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
copyDb("commonnum.db");//拷贝常用号码数据库
ExpandableListView elv = (ExpandableListView) findViewById(R.id.elv_list);
//获取所有常用号码
mList = CommonNumberDao.getCommonNumbers(this);
elv.setAdapter(new MyAdapter());
elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Intent intent = new Intent(Intent.ACTION_DIAL);//进入拨号页面
intent.setData(Uri.parse("tel:" + mList.get(groupPosition).children.get
(childPosition).number));
startActivity(intent);
return false;
}
});
} private class MyAdapter extends BaseExpandableListAdapter {
//返回组的个数
@Override
public int getGroupCount() {
return mList.size();
}
//返回某组孩子个数
@Override
public int getChildrenCount(int groupPosition) {
return mList.get(groupPosition).children.size();
}
//getItem:返回组对象
@Override
public CommonNumberDao.GroupInfo getGroup(int groupPosition) {
return mList.get(groupPosition);
}
//返回孩子对象
@Override
public CommonNumberDao.ChildInfo getChild(int groupPosition, int childPosition) {
return mList.get(groupPosition).children.get(childPosition);
} @Override
public long getGroupId(int groupPosition) {
return groupPosition;
} @Override
public long getChildId(int groupPosition, int childPosition) {
return groupPosition; }
//是否有固定id, 默认就可以,不需要改动
@Override
public boolean hasStableIds() {
return false;
}
//getView:返回组的布局对象
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView view = new TextView(MainActivity.this);
//view.setText("第" + groupPosition + "组");
CommonNumberDao.GroupInfo info = getGroup(groupPosition);
view.setText(info.name);
view.setBackgroundColor(Color.GRAY);
view.setPadding(50, 10, 10, 10);
view.setTextSize(18);
return view;
}
//返回子布局对象
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView view = new TextView(MainActivity.this);
//view.setText("第" + groupPosition + "组" + "-第" + childPosition + "项");
CommonNumberDao.ChildInfo info = getChild(groupPosition, childPosition);
view.setText(info.name + "\n" + info.number);
view.setPadding(10, 10, 10, 10);
view.setTextSize(16);
return view;
}
//孩子是否可以点击
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
private void copyDb(String dbName) {
//拷贝文件, 输入流-->输出流
//输出流
//data/data/包名/files
File filesDir = getFilesDir();
File desFile = new File(filesDir, dbName);//目标文件 //数据库只需要拷贝一次
if (desFile.exists()) {
System.out.println("数据库" + dbName + "已经存在,无需拷贝!");
return;
} AssetManager assets = getAssets();//资产目录管理器 InputStream in = null;
FileOutputStream out = null;
try {
in = assets.open(dbName);//获取资产目录文件的输入流
out = new FileOutputStream(desFile);//输出流 int len = 0;
byte[] buffer = new byte[1024 * 8];
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
} out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} System.out.println("拷贝数据库" + dbName + "完成!!!");
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.loaderman.expandablelistviewdemo.MainActivity">
<!--android:groupIndicator="@null" 可以去掉那个指示箭头-->
<ExpandableListView
android:id="@+id/elv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ExpandableListView>
</LinearLayout>

效果:

利用ExpandableListView实现常用号码查询功能的实现的更多相关文章

  1. Python与数据库[2] -> 关系对象映射/ORM[5] -> 利用 sqlalchemy 实现关系表查询功能

    利用 sqlalchemy 实现关系表查询功能 下面的例子将完成一个通过关系表进行查询的功能,示例中的数据表均在MySQL中建立,建立过程可以使用 SQL 命令或编写 Python 适配器完成. 示例 ...

  2. 利用PHP访问数据库——实现分页功能与多条件查询功能

    1.实现分页功能 <body><table width="100%" border="1">  <thead>    < ...

  3. JQuery常用函数及功能

    JQuery常用函数及功能小结 来源:http://blog.csdn.net/screensky/article/details/7831000 1.文档加载完成执行函数 $(document).r ...

  4. RPM软件包管理的查询功能

    以后大家升级rpm包的时候,不要用Uvh了! 我推荐用Fvh 前者会把没有安装过得包也给装上,后者只会更新已经安装的包   总结:未安装的加上小写p,已安装的不需要加p   查询q    rpm {- ...

  5. 利用kibana插件对Elasticsearch查询

    利用kibana插件对Elasticsearch查询 Elasticsearch是功能非常强大的搜索引擎,使用它的目的就是为了快速的查询到需要的数据. 查询分类: 基本查询:使用Elasticsear ...

  6. Vc数据库编程基础MySql数据库的表查询功能

    Vc数据库编程基础MySql数据库的表查询功能 一丶简介 不管是任何数据库.都会有查询功能.而且是很重要的功能.上一讲知识简单的讲解了表的查询所有. 那么这次我们需要掌握的则是. 1.使用select ...

  7. RPM软件包管理的查询功能 转

    RPM软件包管理的查询功能: 命令格式 rpm {-q|--query} [select-options] [query-options] RPM的查询功能是极为强大,是极为重要的功能之一:举几个常用 ...

  8. Extjs tree 过滤查询功能

    转载: http://blog.csdn.net/xiaobai51509660/article/details/36011899 Extjs4.2中,对于treeStore中未实现filterBy函 ...

  9. 利用solr实现商品的搜索功能

      后期补充: 为什么要用solr服务,为什么要用luncence? 问题提出:当我们访问购物网站的时候,我们可以根据我们随意所想的内容输入关键字就可以查询出相关的内容,这是怎么做到呢?这些随意的数据 ...

随机推荐

  1. 记一次root用户在本地登录及SSH连接均遭遇permission denied的问题排查经过

    某日一位老师反映,机房的6号节点无法登录了.一开始以为是为节点防火墙配置IP白名单时忘记了加进去,但随后发现此节点并未进行白名单配置,密码也一直未有变更,于是在自己的电脑上连接,发现终端里很快显示出了 ...

  2. (十)全志R18 Tina平台关闭所有串口打印的方法

    全志R18 Tina平台关闭所有打印输出方法: 有些国外的产品安全认证,如亚马逊Alexa认证,认证机构会不停地点pcb上的点,看有没有东西输出,有的话就通过这些口想办法破解设备,所以安全认证会要求设 ...

  3. js常用阻止冒泡事件

    原文链接:http://caibaojian.com/javascript-stoppropagation-preventdefault.html 防止冒泡 w3c的方法是e.stopPropagat ...

  4. Linux工具之watch

    watch   watch   监测一个命令的运行结果 -n       指定间隔的时间 -d       watch会高亮显示变化的区域. -t        会关闭watch命令在顶部的时间间隔, ...

  5. Windows bat脚本的for语句

    Windows bat脚本的for语句基本形态如下: 在cmd窗口中:for %I in (command1) do command2 在批处理文件中:for %%I in (command1) do ...

  6. 内嵌tomcat快速入门

    1.依赖 <!--Java语言操作tomcat --> <dependency> <groupId>org.apache.tomcat.embed</grou ...

  7. python 示例代码1

    第一章 python基础一 ​在此不再赘述为什么学习python这门编程,网上搜索一箩筐.我在此仅说一句python的好,用了你就会爱上它. 本python示例代码1000+带你由浅入深的了解pyth ...

  8. mybatic MapperScannerConfigurer的原理

    原文地址:http://www.cnblogs.com/fangjian0423/p/spring-mybatis-MapperScannerConfigurer-analysis.html 前言 本 ...

  9. 移动/Web开发必备工具!DevExtreme v19.1.7火热发布

    DevExtreme Complete Subscription是性能最优的 HTML5,CSS 和 JavaScript 移动.Web开发框架,可以直接在Visual Studio集成开发环境,构建 ...

  10. Vue-指令补充、过滤器、计数器属性、监听属性

    vue实例成员: el | template |data | methods watch 监听事件| computed 计数属性使用 | filters过滤器 | props 父传子 componen ...