在我的应用程序中,我正在实现添加联系人的功能。为此,我使用代码
Intent addNewContact = new Intent(Intent.ACTION_INSERT);
addNewContact.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(addNewContact, ADD_NEW_CONTACT);
添加联系人(在活动结果上)后,我想显示该联系人(仅限,我不想迭代URI),但我不知道如何才能显示该联系人。有办法吗?
这是我的onActivityResult()
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADD_NEW_CONTACT) {
if(resultCode == -1) {
Log.i("New contact Added : ", "Addedd new contact, Need to refress item list");
} else {
Log.i("New contact Added : ", "Canceled to adding new contacts : Not need to update item list");
}
}
}
谢谢!
答案 0 :(得分:3)
您必须阅读onActivityResult()
的{{3}}参数,您可以在其中获取_id,displayName等。
试试此代码
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADD_NEW_CONTACT) {
if(resultCode == -1) {
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
if (cursor.moveToFirst()) {
long newId = cursor.getLong(cursor.getColumnIndexOrThrow(Contacts._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
Log.i("New contact Added", "ID of newly added contact is : " + newId + " Name is : " + name);
}
Log.i("New contact Added : ", "Addedd new contact, Need to refress item list : DATA = " + data.toString());
} else {
Log.i("New contact Added : ", "Canceled to adding new contacts : Not need to update database");
}
}
}
快乐的编码。