Android查询本地联系人主要代码

private ListView list_contact;
private List<HashMap<String, String>> contactlist = new ArrayList<HashMap<String,String>>();
private MyAdapter mAdapter;

android.os.Handler mHandler = new android.os.Handler(){
    @Override
    public void handleMessage(Message msg) {
        mAdapter = new MyAdapter();
        list_contact.setAdapter((ListAdapter) mAdapter);
    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contacts_list);
    initUI();
    initData();
}

private void initData() {

    //读取系统联系人,可能是一个耗时操作,放置到子线程中处理

    new Thread(){
        @Override
        public void run() {
            Uri uri = Uri.parse("content://com.android.contacts/contacts");
            //获取内容解析器对象
            ContentResolver contentResolver = getContentResolver();
            //查询系统联系人数据库表过程(读取联系人权限)
            Cursor cursor = contentResolver.query(uri,new String[]{"_id"},null,null,null );
            //先将列表清空
            contactlist.clear();
            //游标循环提取数据
            while (cursor.moveToNext()){
                String id = cursor.getString(0);
                //根据id值唯一性,查询data表和mimetype表生成的视图,获取data以及mimetype字段
                uri = Uri.parse("content://com.android.contacts/contacts/" + id + "/data");
//如果上面的URI用的是
Uri.parse("content://com.android.contacts/raw_contacts")以及这段的URI使用的是
Uri.parse("content://com.android.contacts/data"),第一次运行不会有问题,当你删除通讯录一个联系人后重新添加是就会报错,the bind value at index 1 is null
                //data2是邮箱
                Cursor indexCursor = contentResolver.query(uri,new String[]{"mimetype","data1","data2"},null,null,null);
                        //循环获取每一个联系人的电话号码以及姓名,数据类型
                HashMap<String,String> hashMap = new HashMap<String,String>();
                while (indexCursor.moveToNext()){
                    String data = indexCursor.getString(1);
                    String type = indexCursor.getString(0);
                                     //区分类型,填充到hashmap里
                    if (type.equals("vnd.android.cursor.item/phone_v2")){
                        if (!TextUtils.isEmpty(data)){
                            hashMap.put("phone",data);

                        }
                    }else if (type.equals("vnd.android.cursor.item/name")){
                        hashMap.put("name",data);
                    }
                }
                indexCursor.close();
                contactlist.add(hashMap);
            }
            cursor.close();
           // 消息机制,发送一个空的消息,告知主线程可以去使用子线程已经填充好的数据集合
            mHandler.sendEmptyMessage(0);

        };
    }.start();
}

private void initUI() {
    list_contact = (ListView) findViewById(R.id.list_contact);
    list_contact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //获取点中条目的索引指向集合中的对象
            if (mAdapter!=null){
                HashMap<String,String> hashMap = mAdapter.getItem(position);
                //获取当前条目集合中的电话号码
                String phone = hashMap.get("phone");
                //在结束此界面回到前一个导航界面的时候,需要将号码返回第三个界面
                Intent intent = new Intent();
                intent.putExtra("phone",phone);
                setResult(0, intent);
                finish();
            }
        }
    });
}

private class MyAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return contactlist.size();
    }

    @Override
    public HashMap<String,String> getItem(int position) {
        return contactlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = View.inflate(getApplication(),R.layout.listview_contact_item,null);
        //不能忘记添加view. 不然报空指针
        TextView contactName = (TextView) view.findViewById(R.id.contact_name);
        TextView contactNum = (TextView) view.findViewById(R.id.contact_num);
        contactName.setText(getItem(position).get("name"));
        contactNum.setText(getItem(position).get("phone"));
        return view;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值