我目前正在与 Android 联系人内容提供商合作,目前可以使用以下代码毫无问题地访问联系人的完整显示名称:
String[] PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
String SELECTION = "LOWER(" + ContactsContract.Contacts.DISPLAY_NAME + ")"
+ " LIKE '" + constraint + "%' " + "and " +
ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
Cursor cur = managedQuery(ContactsContract.Contacts.CONTENT_URI,
PROJECTION, SELECTION, null, sortOrder);
但是我希望能够分别获取联系人的名字和姓氏,我尝试使用 StructuredName 来尝试获取它,但我似乎无法让它工作。
谁能指出正确的方向,告诉我如何正确使用 StructuredName 将名称拆分为名字和姓氏?
更新:
按照 hovanessyan 的建议,我尝试了以下操作:
String[] PROJECTION = new String[] {
ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.HAS_PHONE_NUMBER,
};
String SELECTION = ContactsContract.CommonDataKinds.StructuredName.IN_VISIBLE_GROUP + " = '1'";
String sortOrder = ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, sortOrder);
int indexGivenName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
int indexFamilyName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
int indexDisplayName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
while (cursor.moveToNext()) {
String given = cursor.getString(indexGivenName);
String family = cursor.getString(indexFamilyName);
String display = cursor.getString(indexDisplayName);
Log.e("XXX", "Name: | " + given + " | " + family + " | " + display);
}
但是使用 PROJECTION 会导致崩溃,如下所示:
12-08 16:52:01.575: E/AndroidRuntime(7912): Caused by: java.lang.IllegalArgumentException: Invalid column has_phone_number
如果我删除 PROJECTION,我会打印出所有结果,但其中很多包含 NULL。
例如:
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
所以任何人都可以看出我做错了什么,因为我的投影不起作用吗?
进一步更新:
我已经解决了我的 PROJECTION 问题,但现在我遇到了一个问题,即 DATA 内容提供者向我提供所有空数据并导致我的代码中出现 NULL 指针异常。
例如,来自 ContactsContract.Contacts 的游标计数返回 115,但使用 DATA 表返回 464 使用相同的参数,这在我的应用程序中造成了巨大的问题。
有人知道这是为什么吗?
最佳答案
看看ContactsContract.CommonDataKinds.StructuredName类(class)。你有所有需要的列,你可以做类似的事情:
Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, other_query_params_for_filtering);
int indexGivenName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
int indexFamilyName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
int indexDisplayName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
while (cursor.moveToNext()) {
String given = cursor.getString(indexGivenName);
String family = cursor.getString(indexFamilyName);
String display = cursor.getString(indexDisplayName);
}
关于android - 获取联系人的名字和姓氏而不是单个显示名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8431897/