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

来源:互联网 发布:大型3d网络手游 编辑:程序博客网 时间:2024/05/21 10:18
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);    }};@Overrideprotected 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
原创粉丝点击