项目需要做到这个效果,是读取系统联系人及其邮箱地址,选中后进行邮件分享的功能。
在读取联系人时,QC提到并未排序,最开始说,数字、英文、中文(中文按笔画排序)我说,按正常人,都是拼音排序的吧?谁知道人家说,我们香港那边都是按笔画排序的(*  ̄︿ ̄),谈论到最后,说,数字、英文、中文(按拼音排序),起初答应了,后来觉得太烦,这个东西本身不是客户的需求,只是QC自以为是的想法,觉得置之不理,以系统为准,系统怎么排,就怎么排。
原本我获取系统联系人及其邮箱的代码:
/** * 读取联系人的信息 * Gift */ public static List这里用到的 ContactsBean 的实体类的内容为:readContactsEmail(Context mContext) { List contactsBeans = new ArrayList<>(); ContactsBean contactsBean; Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); int contactIdIndex = 0; int nameIndex = 0; if (cursor.getCount() > 0) { contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID); nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); } while (cursor.moveToNext()) { contactsBean = new ContactsBean(); String contactId = cursor.getString(contactIdIndex); contactsBean.setName(cursor.getString(nameIndex)); /* * 查找该联系人的email信息 */ Cursor emails = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId, null, null); int emailIndex = 0; if (emails.getCount() > 0) { emailIndex = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA); } while (emails.moveToNext()) { contactsBean.setEmail(emails.getString(emailIndex)); } if (!StringUtils.isBlank(contactsBean.getEmail())) { contactsBeans.add(contactsBean); } } return contactsBeans; }
String name = ""; String email = "";
http://blog.chinaunix.net/uid-23023613-id-3023387.html
通过这位博主的资料,我得知 :
通讯录的数据库文件contacts2.db的raw_contacts表。用sqlitedbviewer工具打开后可以看到有这么一个字段:sort_key(sort_key_alt与之相似,只不过是英文环境下有个按given name还是first name排序的问题)。
正常状况下,我们新建一个联系人的话,如果是英文,则sort_key与display_name字段显示相同,如果输入姓名是中文,如“你好”,sort_key则会显示“ni 你 hao 好”,这样开发者既可以根据此字段按拼音排序,中英文混排,以及按拼音搜索联系人拉。
既然有这个字段,那就好办了。通过查看
ContactsContract.Contacts
的源码,发现:
/** * Sort key that takes into account locale-based traditions for sorting * names in address books. The default * sort key is {@link #DISPLAY_NAME_PRIMARY}. For Chinese names * the sort key is the name's Pinyin spelling, and for Japanese names * it is the Hiragana version of the phonetic name. */ public static final String SORT_KEY_PRIMARY = "sort_key";解释也很明朗。于是我将上面获取的方法的代码改为:
/** * 读取联系人的信息 * Gift */ public static ListreadContactsEmail(Context mContext) { List contactsBeans = new ArrayList<>(); ContactsBean contactsBean; Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.SORT_KEY_PRIMARY); int contactIdIndex = 0; int nameIndex = 0; int sort_key = 0; if (cursor.getCount() > 0) { contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID); nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); sort_key = cursor.getColumnIndex(ContactsContract.Contacts.SORT_KEY_PRIMARY); } while (cursor.moveToNext()) { contactsBean = new ContactsBean(); String contactId = cursor.getString(contactIdIndex); contactsBean.setName(cursor.getString(nameIndex)); contactsBean.setSort_key(getSortKey(cursor.getString(sort_key))); /* * 查找该联系人的email信息 */ Cursor emails = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId, null, null); int emailIndex = 0; if (emails.getCount() > 0) { emailIndex = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA); } while (emails.moveToNext()) { contactsBean.setEmail(emails.getString(emailIndex)); } if (!StringUtils.isBlank(contactsBean.getEmail())) { contactsBeans.add(contactsBean); } } return contactsBeans; }
附上getSortKey方法:
/** * 获取sort key的首个字符,并将其转换成大写,如果是英文字母就直接返回,否则返回#。 * @param str_sortKey 数据库中读取出的sort key * @return 首字母、# * Gift */ private static String getSortKey(String str_sortKey) { String sort_key = str_sortKey.substring(0, 1).toUpperCase(); if (sort_key.matches("[A-Z]")) { return sort_key; } return "#"; }
这样返回回来,就是已经按sortKey排好序的列表了。重点在于
Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.SORT_KEY_PRIMARY);
最后一个传值。
/** * Query the given URI, returning a {@link Cursor} over the result set. ** For best performance, the caller should follow these guidelines: *
* Provide an explicit projection, to prevent * reading data from storage that aren't going to be used. * Use question mark parameter markers such as 'phone=?' instead of * explicit values in the {@code selection} parameter, so that queries * that differ only by those values will be recognized as the same * for caching purposes. * * * * @param uri The URI, using the content:// scheme, for the content to * retrieve. * @param projection A list of which columns to return. Passing null will * return all columns, which is inefficient. * @param selection A filter declaring which rows to return, formatted as an * SQL WHERE clause (excluding the WHERE itself). Passing null will * return all rows for the given URI. * @param selectionArgs You may include ?s in selection, which will be * replaced by the values from selectionArgs, in the order that they * appear in the selection. The values will be bound as Strings. * @param sortOrder How to order the rows, formatted as an SQL ORDER BY * clause (excluding the ORDER BY itself). Passing null will use the * default sort order, which may be unordered. * @return A Cursor object, which is positioned before the first entry, or null * @see Cursor */ public final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { return query(uri, projection, selection, selectionArgs, sortOrder, null); }
传入sort_key,按sort_key排序,传null可能是无序的,但我起初传null的时候,是按我添加通讯录时的顺序返回的,应该是按添加时间排序。
但不论如何,我们所要的效果已经达到了。