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. FlowNet2.0论文笔记

    原论文标题:FlowNet 2.0: Evolution of Optical Flow Estimation with Deep Networks 文章是对FlowNet的进一步改进,主要贡献为如下 ...

  2. 使用比特币轻量钱包Electrum

    一.选择对应的钱包安装程序 浏览器打开 https://electrum.org/#download 选择windwos版本的安装程序 点击下载,并安装 二.运行Electrum的 testnet版本 ...

  3. Beta冲刺——星期四

    这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 团队名称 飞猪们 这个作业的目标 剩余任务预估,分配任务(开发,测试等).按要求提交当天冲刺报告. ...

  4. 分析bug是前端还是后端的

    如何分析一个bug是前端还是后端的? 平常提bug的时候,前端开发和后端开发总是扯皮,不承认是对方的bug 这种情况很容易判断,先抓包看请求报文,对着接口文档,看请求报文有没问题,有问题就是前端发的数 ...

  5. 查看ocx控件CLSID的方法(转载)

    CLSID就是classID类的标识码 1.打开注册表,window + r ,输入regedit,确定 2.点击 编辑 选择查找 3.ok拉 参考:https://blog.csdn.net/u01 ...

  6. pycharm 怎么能像在命令行中输入参数进行调试

    pycharm中配置main参数 Run->Edit Configurations->Script Parames 把需要在xxx.py A B C 后面的参数输入到如下位置. 否则会报错 ...

  7. ESP8266常见问题汇总——转载自官网

    ESP8266 常见问题 本页面收集esp8266常见问题 概述 本文档主要介绍开发者在ESP8266开发中常见的一些问题. 这些问题主要包括以下几大类: 基本概念相关 ESP8266 相关 AiCl ...

  8. php类知识点滴---类的实例化,构造函数,继承初步

    实例化类----黑科技用法,通过字符串来实例化 class coach { public function __construct() { echo "欢迎光临北武堂"." ...

  9. 使用Apache Curator管理ZooKeeper(转)

    Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...

  10. 01_3大配置管理工具、SaltStack安装、修改minion_id

    1.配置管理 1.1 puppet /'pʌpɪt/  木偶:傀儡:受他人操纵的人 使用自有的puppet描述语言,可管理配置文件.用户.cron任务.软件包.系统服务等. 问题: 学习曲线非常陡峭 ...