利用ExpandableListView实现常用号码查询功能的实现
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实现常用号码查询功能的实现的更多相关文章
- Python与数据库[2] -> 关系对象映射/ORM[5] -> 利用 sqlalchemy 实现关系表查询功能
利用 sqlalchemy 实现关系表查询功能 下面的例子将完成一个通过关系表进行查询的功能,示例中的数据表均在MySQL中建立,建立过程可以使用 SQL 命令或编写 Python 适配器完成. 示例 ...
- 利用PHP访问数据库——实现分页功能与多条件查询功能
1.实现分页功能 <body><table width="100%" border="1"> <thead> < ...
- JQuery常用函数及功能
JQuery常用函数及功能小结 来源:http://blog.csdn.net/screensky/article/details/7831000 1.文档加载完成执行函数 $(document).r ...
- RPM软件包管理的查询功能
以后大家升级rpm包的时候,不要用Uvh了! 我推荐用Fvh 前者会把没有安装过得包也给装上,后者只会更新已经安装的包 总结:未安装的加上小写p,已安装的不需要加p 查询q rpm {- ...
- 利用kibana插件对Elasticsearch查询
利用kibana插件对Elasticsearch查询 Elasticsearch是功能非常强大的搜索引擎,使用它的目的就是为了快速的查询到需要的数据. 查询分类: 基本查询:使用Elasticsear ...
- Vc数据库编程基础MySql数据库的表查询功能
Vc数据库编程基础MySql数据库的表查询功能 一丶简介 不管是任何数据库.都会有查询功能.而且是很重要的功能.上一讲知识简单的讲解了表的查询所有. 那么这次我们需要掌握的则是. 1.使用select ...
- RPM软件包管理的查询功能 转
RPM软件包管理的查询功能: 命令格式 rpm {-q|--query} [select-options] [query-options] RPM的查询功能是极为强大,是极为重要的功能之一:举几个常用 ...
- Extjs tree 过滤查询功能
转载: http://blog.csdn.net/xiaobai51509660/article/details/36011899 Extjs4.2中,对于treeStore中未实现filterBy函 ...
- 利用solr实现商品的搜索功能
后期补充: 为什么要用solr服务,为什么要用luncence? 问题提出:当我们访问购物网站的时候,我们可以根据我们随意所想的内容输入关键字就可以查询出相关的内容,这是怎么做到呢?这些随意的数据 ...
随机推荐
- 【Day1】3.数据类型
视频地址(全部) https://edu.csdn.net/course/detail/26057 课件地址(全部) https://download.csdn.net/download/gentl ...
- Win10带有网络连接的安全模式怎么开启?
安全模式是在Windows系统中不加载第三方设备驱动程序的情况下启动电脑,从而可以方便的检测与修复电脑系统的错误,比如在安全模式下可以删除某些顽固的文件.查杀病毒.修复系统故障.卸载恶意软件等.不过在 ...
- 重新编译mysqldump,使mysqldump具有进度输出功能
重新编译mysql,使mysqldump具有进度输出功能 安装编译过程所必须的依赖包和环境 yum install -y gcc cmake boost boost-build boost-devel ...
- jquery中.each()方法遍历循环如何终止方法
使用return false 终止循环 function checkStar(obj){ var flag=false; //获取本节点星级 var star = obj.getAttribute(& ...
- MySQL 8下忘密码后重置密码
解决方案:1):设置mysql为无密码启动 (修改MySQL的登录设置:vi /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-table) 2):重新启动mys ...
- 网卡绑定(bonding)
就是将多块网卡绑定同一IP地址对外提供服务,可以实现高 可用或者负载均衡.当然,直接给两块网卡设置同一IP地址 是不可能的.通过bonding,虚拟一块网卡对外提供连接, 物理网卡的被修改为相同的MA ...
- Vue学习日记(四)——Vue状态管理vuex
前言 先说句前话,如果不是接触大型项目,不需要有多个子页面,不使用vuex也是完全可以的. 说实在话,我在阅读vuex文档的时候,也很难以去理解vuex,甚至觉得没有使用它我也可以.但是直到我在项目碰 ...
- HDU-1358-Period(KMP, 循环节)
链接: https://vjudge.net/problem/HDU-1358#author=0 题意: For each prefix of a given string S with N char ...
- JAVA访问Zabbix API
Zabbix 一.Zabbix 概述 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的 ...
- kubernetes1.17集群部署
学习自:https://www.jianshu.com/p/789bc867feaa ###批量配置免秘钥 密码可自行修改 这里的密码是123456 yum install -y expect ss ...