22、高级工具--常用号码查询,ExpandableListView的使用
2014-01-14 17:33
519 查看
实现效果图:
这些常用电话号码是存储在数据库中的,这里直接使用assets/commonnum.db
创建CommonNumberActivity以及布局文件,代码:
布局文件:
代码中使用的获取分组名、个数、分组条目个数、条目名称等功能封装在CommonNumberDao类中,其实就是对数据库的一个操作使用,代码:
这些常用电话号码是存储在数据库中的,这里直接使用assets/commonnum.db
创建CommonNumberActivity以及布局文件,代码:
package com.example.mobilesafe; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; import com.example.mobilesafe.db.CommonNumberDao; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by sing on 14-1-10. * desc: */ public class CommonNumberActivity extends Activity { public static final String TAG = "CommonNumberActivity"; private ExpandableListView elv_common_number; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.commonnumberactivity_layout); elv_common_number = (ExpandableListView) findViewById(R.id.elv_common_number); //设置适配器 elv_common_number.setAdapter(new CommonNumberAdapter()); //点击分组中的孩子view时的处理事件 elv_common_number.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i2, long l) { //取号码 String number = ((TextView) view).getText().toString().split("\n")[1]; //拨号 Intent intent = new Intent(); intent.setAction(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:" + number)); startActivity(intent); return false; } }); } private class CommonNumberAdapter extends BaseExpandableListAdapter { private List<String> groupNames; private Map<Integer,List<String>> childrenCache; public CommonNumberAdapter() { childrenCache = new HashMap<Integer, List<String>>(); } @Override public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) { TextView tv; if (view==null) { tv = new TextView(getApplicationContext()); }else { tv = (TextView) view; } tv.setTextSize(20); String name = null; if (childrenCache.containsKey(i)) { name = childrenCache.get(i).get(i2); }else { List<String> results = CommonNumberDao.getChildrenNamesByPosition(i); childrenCache.put(i, results); name = results.get(i2); } tv.setText(name); return tv; } /** * 返回true表示分组条目可以响应单击事件 * @param i * @param i2 * @return */ @Override public boolean isChildSelectable(int i, int i2) { return true; } /** * 返回组数 * @return */ @Override public int getGroupCount() { return CommonNumberDao.getGroupCount(); } /** * 一组有多少条目 * @param i * @return */ @Override public int getChildrenCount(int i) { return CommonNumberDao.getChildrenCount(i); } @Override public boolean hasStableIds() { return false; } @Override public Object getChild(int i, int i2) { return null; } @Override public long getGroupId(int i) { return i; } @Override public Object getGroup(int i) { return null; } @Override public long getChildId(int i, int i2) { return i2; } @Override public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { TextView tv; if (view==null) { tv = new TextView(getApplicationContext()); }else { tv = (TextView) view; } tv.setTextSize(28); if (groupNames == null) { groupNames = CommonNumberDao.getGroupNames(); } tv.setText(" " + groupNames.get(i)); return tv; } } }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView style="@style/title_center_text" android:text="常用号码" /> <View style="@style/dot_splitter_view"/> <ExpandableListView android:id="@+id/elv_common_number" android:layout_height="match_parent" android:layout_width="match_parent"/> </LinearLayout>
代码中使用的获取分组名、个数、分组条目个数、条目名称等功能封装在CommonNumberDao类中,其实就是对数据库的一个操作使用,代码:
package com.example.mobilesafe.db; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList; import java.util.List; /** * Created by sing on 14-1-15. * desc: */ public class CommonNumberDao { private static final String TAG = "CommonNumberDao"; public static final String FILE_DIR = "/data/data/com.example.mobilesafe/files/"; /** * 获取常用号码分组数 * @return */ public static int getGroupCount() { int count = 0; String path = FILE_DIR + "commonnum.db"; SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); if (db.isOpen()) { Cursor cursor = db.rawQuery("select * from classlist", null); count = cursor.getCount(); cursor.close(); db.close(); } return count; } /** * 获取常用号码分组名 * @return */ public static List<String> getGroupNames() { List<String> groupNames = new ArrayList<String>(); String path = FILE_DIR + "commonnum.db"; SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); if (db.isOpen()) { Cursor cursor = db.rawQuery("select name from classlist", null); while (cursor.moveToNext()) { groupNames.add(cursor.getString(0)); } cursor.close(); db.close(); } return groupNames; } /** * 获取指定分组的名称 * @param groupPosition * @return */ public static String getGroupNameByPosition(int groupPosition) { String name = null; String path = FILE_DIR + "commonnum.db"; SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); if (db.isOpen()) { Cursor cursor = db.rawQuery("select name from classlist where idx=?", new String[]{(groupPosition + 1) + ""}); cursor.moveToFirst(); name = cursor.getString(0); cursor.close(); db.close(); } return name; } /** * 获取指定分组的条目个数 * @param groupPosition * @return */ public static int getChildrenCount(int groupPosition) { int count = 0; String path = FILE_DIR + "commonnum.db"; SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); if (db.isOpen()) { Cursor cursor = db.rawQuery("select * from table" + (groupPosition + 1), null); count = cursor.getCount(); cursor.close(); db.close(); } return count; } /** * 获取指定分组指定条目的名称和电话号码 * @param groupPosition * @param childrenPosition * @return */ public static String getChildNameByPosition(int groupPosition, int childrenPosition) { String result = null; String path = FILE_DIR + "commonnum.db"; SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); if (db.isOpen()) { Cursor cursor = db.rawQuery("select name,number from table" + (groupPosition + 1) + " where _id=?", new String[]{(childrenPosition + 1) + ""}); if (cursor.moveToFirst()) { result = cursor.getString(0) + "\n" + cursor.getString(1); } cursor.close(); db.close(); } return result; } /** * 获取某分组下所有的项目名称和电话号码 * @param groupPosition * @return */ public static List<String> getChildrenNamesByPosition(int groupPosition) { List<String> results = new ArrayList<String>(); String path = FILE_DIR + "commonnum.db"; SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); if (db.isOpen()) { Cursor cursor = db.rawQuery("select name,number from table" + (groupPosition + 1), null); while (cursor.moveToNext()) { results.add(cursor.getString(0) + "\n" + cursor.getString(1)); } cursor.close(); db.close(); } return results; } }
相关文章推荐
- Android常用控件(能折叠的ListView)--ExpandableListView的使用模仿QQ好友列表
- [安卓]手机管家(九)高级工具之号码归属地查询
- android 高级之旅 (六)ExpandableListView 使用详解
- 17、高级工具--号码归属地查询
- 如何快速查询python中常用包和使用办法
- 6. 使用高级查询技术
- zdz工具箱v1.5 android版本发布了,集成各种个人生活中常用的工具,方便日常使用管理
- 数据库中如何使用SQL查询连续号码段
- Android ExpandableListView的使用
- 几个常用监控工具的使用
- linux工具之curl与wget高级使用
- android开发之ExpandableListView的使用,实现类似QQ好友列表
- chrome调试工具高级不完整使用指南(实战二)
- Wireshark抓包工具使用教程以及常用抓包规则
- [置顶] 当SearchView搜索的listView使用自定义适配器时,自定义setFilterText()方法(模糊查询筛选)
- Lucene使用(三)常用的查询规则
- 树形组件ExpandableListView的使用介绍
- TensorFlow高级API(tf.contrib.learn)及可视化工具TensorBoard的使用
- 一起学DNS系列(十四)DNS查询工具之DIG的使用(1)