首页 > 代码库 > 利用ExpandableListView实现常用号码查询功能的实现

利用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实现常用号码查询功能的实现