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
    评论
好的,您想了解安卓开发 ExpandableListView使用示例,以下是一个简单的示例: 1. 定义适配器类 ``` public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context mContext; private List<String> mGroupTitles; // 父分组数据 private HashMap<String, List<String>> mChildData; // 子分组数据 public ExpandableListAdapter(Context context, List<String> groupTitles, HashMap<String, List<String>> childData) { mContext = context; mGroupTitles = groupTitles; mChildData = childData; } @Override public int getGroupCount() { return mGroupTitles.size(); } @Override public int getChildrenCount(int groupPosition) { return mChildData.get(mGroupTitles.get(groupPosition)).size(); } @Override public Object getGroup(int groupPosition) { return mGroupTitles.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return mChildData.get(mGroupTitles.get(groupPosition)).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.list_item_group, null); TextView title = (TextView) view.findViewById(R.id.group_title); title.setText(mGroupTitles.get(groupPosition)); return view; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.list_item_child, null); TextView title = (TextView) view.findViewById(R.id.child_title); title.setText(mChildData.get(mGroupTitles.get(groupPosition)).get(childPosition)); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } ``` 2. 定义布局文件 list_item_group.xml ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="40dp" android:paddingRight="40dp" android:paddingTop="20dp" android:paddingBottom="20dp"> <TextView android:id="@+id/group_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout> ``` list_item_child.xml ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="40dp" android:paddingRight="40dp" android:paddingTop="15dp" android:paddingBottom="15dp"> <TextView android:id="@+id/child_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" /> </LinearLayout> ``` 3. 在 Activity 中使用 ExpandableListView ``` public class MainActivity extends AppCompatActivity { private ExpandableListView mExpandableListView; private ExpandableListAdapter mAdapter; private List<String> mGroupTitles; private HashMap<String, List<String>> mChildData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化数据 initData(); // 初始化 ExpandableListView mExpandableListView = (ExpandableListView) findViewById(R.id.expandable_list_view); mAdapter = new ExpandableListAdapter(this, mGroupTitles, mChildData); mExpandableListView.setAdapter(mAdapter); // 设置子项点击监听器 mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(MainActivity.this, "您点击了" + mChildData.get(mGroupTitles.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show(); return true; } }); } private void initData() { mGroupTitles = new ArrayList<>(); mGroupTitles.add("计算机"); mGroupTitles.add("自然科学"); mGroupTitles.add("哲学"); List<String> computers = new ArrayList<>(); computers.add("Java"); computers.add("Python"); computers.add("C++"); computers.add("PHP"); computers.add("JavaScript"); computers.add("HTML/CSS"); computers.add("SQL"); List<String> sciences = new ArrayList<>(); sciences.add("物理"); sciences.add("化学"); sciences.add("生物"); sciences.add("地理"); sciences.add("天文"); List<String> philosophies = new ArrayList<>(); philosophies.add("伦理学"); philosophies.add("形而上学"); philosophies.add("认识论"); philosophies.add("逻辑学"); philosophies.add("美学"); mChildData = new HashMap<>(); mChildData.put(mGroupTitles.get(0), computers); mChildData.put(mGroupTitles.get(1), sciences); mChildData.put(mGroupTitles.get(2), philosophies); } } ``` 这样,您就可以展示一个带父分组和子分组的 ExpandableListView ,同时设置子项点击监听器,实现相应的功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

asmcvc

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

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

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

打赏作者

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

抵扣说明:

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

余额充值