1

在我的应用程序中,我正在实现添加联系人的功能。为此,我使用代码作为

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");
            }
        }
    }

谢谢!

4

1 回答 1

3

您必须读取数据 参数onActivityResult(),您可以在其中获取_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");
            }
        }
    }

快乐编码。

于 2011-07-18T08:13:32.817 回答