标签:
android系统的联系人数据库是非常的复杂,并不是一个简单的表就能搞定的。
要简单的查询用户名和电话号码,首先要查询到姓名,然后再进局姓名去查询电话号码,一个姓名可能对应多个电话号码:
// These are the Contacts rows that we will retrieve. static final String[] CONTACTS_SUMMARY_PROJECTION = new String[]{ ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.CONTACT_STATUS, ContactsContract.Contacts.CONTACT_PRESENCE, ContactsContract.Contacts.PHOTO_ID, ContactsContract.Contacts.LOOKUP_KEY, } Uri baseUri = ContactsContract.Contacts.CONTENT_URI; // Now create and return a CursorLoader that will take care of // creating a Cursor for the data being displayed. String select = "((" + ContactsContract.Contacts.DISPLAY_NAME + " NOTNULL) AND (" + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1) AND (" + ContactsContract.Contacts.DISPLAY_NAME + " != ‘‘ ))"; return new CursorLoader(ContactActivity.this.getApplicationContext(), baseUri, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); List<Map<String, String>> list = new ArrayList<>(); while (data.moveToNext()) { Map<String, String> map = new HashMap(); String name = data.getString(data.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); String ContactId = data.getString(data.getColumnIndex(ContactsContract.Contacts._ID)); Cursor phone = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null); while (phone.moveToNext()) { String Number = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); if (Number != null) { map.put(name, Number); } } list.add(map); }
标签:
原文地址:http://www.cnblogs.com/fangjianbin/p/4390559.html