我一直试图获得一个联系人的昵称现在几个小时,仍然无法让他们工作,我被告知他们在电话号码等不同的表。但我不知道如何访问它们。
我最接近的是这个..
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID +" = ?", new String[]{String.valueOf(recordId)}, null);
while (cursor.moveToNext()) {
Cursor nickname = context.getContentResolver().query( ContactsContract.Data.CONTENT_URI, null, ContactsContract.CommonDataKinds.Nickname.CONTACT_ID +" = "+ recordId, null, null);
while (nickname.moveToNext()) {
try {
String nicknameName = nickname.getString(nickname.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME));
String nicknameType = nickname.getString(nickname.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.TYPE));
switch (Integer.valueOf(nicknameType)) {
case 1: nicknameType = "TYPE_HOME"; break;
}
list.add(new KeyValue("Nickname:" + nicknameType, nicknameName));
} catch (Exception e) { continue; }
}
nickname.close();
}
这会获取联系人的所有数据以及类型,例如:Thomas Owers
1
这一切都很好,但它不会给我什么数据,所以它给出了电子邮件,电话,名字,昵称,但我不能区分它们。
任何帮助都会有很大帮助,谢谢! :)
答案 0 :(得分:5)
我在搜索互联网几个小时后得到了昵称......
ArrayList<KeyValue> list = new ArrayList<KeyValue>();
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID +" = ?", new String[]{String.valueOf(recordId)}, null);
while (cursor.moveToNext()) {
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] params = new String[] {String.valueOf(recordId), ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE};
Cursor nickname = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, params, null);
while (nickname.moveToNext()) {
String nicknameName = nickname.getString(nickname.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME));
String nicknameType = nickname.getString(nickname.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.TYPE));
switch (Integer.valueOf(nicknameType)) {
case 1: nicknameType = "Default"; break;
case 2: nicknameType = "OtherName"; break;
case 3: nicknameType = "MaidenName"; break;
case 4: nicknameType = "ShortName"; break;
case 5: nicknameType = "Initials"; break;
}
list.add(new KeyValue("Nickname:" + nicknameType, nicknameName));
}
nickname.close();
}
return list;
此代码获得昵称! :)
答案 1 :(得分:0)
Donot获取所有数据。只能使用投影
获取所需数据String[] proj ={ContactsContract.CommonDataKinds.Nickname.NAME, ContactsContract.CommonDataKinds.Nickname.TYPE};
Cursor nickname = getContentResolver().query( ContactsContract.Data.CONTENT_URI, proj,ContactsContract.CommonDataKinds.Nickname.CONTACT_ID +" = "+ recordId, null, null);