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. python学习笔记-(十三)线程、进程、多线程&多进程

    为了方便大家理解下面的知识,可以先看一篇文章:http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html 线程 1.什么是线程? ...

  2. Gym - 101955E The Kouga Ninja Scrolls (曼哈顿距离变换+线段树)

    题意:有n个忍者(编号为1-n),每个忍者有三个属性:横坐标x,纵坐标y,所属门派c,要求支持三种操作: 1.改变第k个忍者的位置 2.改变第k个忍者的门派 3.查询编号为[l,r]之间的忍者中,所属 ...

  3. vue项目中,点击按钮复制其内容

    <el-table-column label="推广链接" align="center"> <template slot-scope=&quo ...

  4. [人物存档]【AI少女】【捏脸数据】精灵

    AISChaF_20191101204710076.png

  5. RPM软件管理

    1.源代码形式 绝大多数软件都是以源代码形式发布的:     因为开源的理念是不重复造轮子:让其它不以商业为目的人都能修改这个软件:   源代码一般会被打包成tar.gz的压缩归档文件: 程序源代码需 ...

  6. jquery clearQueue方法 语法

    jquery clearQueue方法 语法 作用:clearQueue() 方法停止队列中所有仍未执行的函数.与 stop() 方法不同,(只适用于动画),clearQueue() 能够清除任何排队 ...

  7. idea 2017 破解方法

    一.先进入Intellij IDEA的官网:https://www.jetbrains.com,下载安装 二.破解. 网上的破解方法较多,总结下来大概有下面几种办法供大家作为参考 声明:破解用于学习和 ...

  8. Lucas(卢卡斯)定理

    Lucas定理 对于C(m,n)%P(P是质数)这样的问题,可以通过预处理阶乘和阶乘的逆元,来快速计算.但是当m,n大于P时,就不能保证m,n与P互质了,但不互质的情况下,乘法逆元不存在,此时就需要卢 ...

  9. sql注入的基本小知识

    load_fie('') into outfile '' into dumpfile('') 堆叠注入 ;insert into liunx密码读取 /etc/passwd /etc/shadow W ...

  10. 04.重建二叉树 (Java)

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...