如何获取外来词的基本字母表并像在设备本地联系人中一样对它们进行相应排序?
How to get the base alphabet of a foreign word and sort them accordingly like in device local contact?
例如,假设本地联系人有多个日语联系人,姓名为たなか、つなき、テルテル。在本地联系人中,这些名称将在部分索引字母 た 下排序。
根据我的阅读,可以通过整理对它们进行排序,但是如何将它们排序到索引 た 甚至知道它们应该进入 た 索引?
谢谢。
API 有一个返回字母列表的技巧,还可以选择包括每个字符的计数。
参见 EXTRA_ADDRESS_BOOK_INDEX
的官方文档:
https://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#EXTRA_ADDRESS_BOOK_INDEX
示例代码:
Uri uri = Contacts.CONTENT_URI.buildUpon()
.appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true")
.build();
Cursor cursor = getContentResolver().query(uri,
new String[] {Contacts.DISPLAY_NAME},
null, null, null);
Bundle bundle = cursor.getExtras();
if (bundle.containsKey(Contacts.EXTRA_ADDRESS_BOOK_INDEX_TITLES) && bundle.containsKey(Contacts.EXTRA_ADDRESS_BOOK_INDEX_COUNTS)) {
String sections[] = bundle.getStringArray(Contacts.EXTRA_ADDRESS_BOOK_INDEX_TITLES);
// sections will now contain an array of characters that represent the first letter of each available contact name
// optionally - if you need counts per letter - int counts[] = bundle.getIntArray(Contacts.EXTRA_ADDRESS_BOOK_INDEX_COUNTS);
}
例如,假设本地联系人有多个日语联系人,姓名为たなか、つなき、テルテル。在本地联系人中,这些名称将在部分索引字母 た 下排序。
根据我的阅读,可以通过整理对它们进行排序,但是如何将它们排序到索引 た 甚至知道它们应该进入 た 索引?
谢谢。
API 有一个返回字母列表的技巧,还可以选择包括每个字符的计数。
参见 EXTRA_ADDRESS_BOOK_INDEX
的官方文档:
https://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#EXTRA_ADDRESS_BOOK_INDEX
示例代码:
Uri uri = Contacts.CONTENT_URI.buildUpon()
.appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true")
.build();
Cursor cursor = getContentResolver().query(uri,
new String[] {Contacts.DISPLAY_NAME},
null, null, null);
Bundle bundle = cursor.getExtras();
if (bundle.containsKey(Contacts.EXTRA_ADDRESS_BOOK_INDEX_TITLES) && bundle.containsKey(Contacts.EXTRA_ADDRESS_BOOK_INDEX_COUNTS)) {
String sections[] = bundle.getStringArray(Contacts.EXTRA_ADDRESS_BOOK_INDEX_TITLES);
// sections will now contain an array of characters that represent the first letter of each available contact name
// optionally - if you need counts per letter - int counts[] = bundle.getIntArray(Contacts.EXTRA_ADDRESS_BOOK_INDEX_COUNTS);
}