22、高级工具--常用号码查询,ExpandableListView的使用

实现效果图:


这些常用电话号码是存储在数据库中的,这里直接使用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;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

asmcvc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值