如何使用联系人ID编辑nativecontacts字段

时间:2012-12-20 09:02:57

标签: android native edit

如何使用联系人ID以编程方式在Android中编辑本机联系人中的名字,姓氏,移动电话号码,照片,电子邮件,地址。请帮我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

按钮上的

单击执行以下操作:

        Intent in = new Intent(Intent.ACTION_INSERT_OR_EDIT);
        in.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
        startActivityForResult(in,EDIT_CONTACT);

并在onActivityResult函数中执行此操作:

案例EDIT_CONTACT:

            if (resultCode == RESULT_OK) {
                Uri contactData = data.getData();
                Cursor cur = managedQuery(contactData, null, null, null, null);
                ContentResolver contect_resolver = getContentResolver();

                if (cur.moveToFirst()) {
                    String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                    String name = "";
                    String no = "";
                    String key = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts.LOOKUP_KEY));


                    String where = ContactsContract.Data.DISPLAY_NAME + " = ? AND " + 
                    ContactsContract.Data.MIMETYPE + " = ? AND " +
                    String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE) + " = ? ";
                    String[] params = new String[] {name,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
                    String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)};

                    Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);

                    if (phoneCur.moveToFirst()) {
                        name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        key = phoneCur.getString(phoneCur.getColumnIndexOrThrow(ContactsContract.Contacts.LOOKUP_KEY));

                        System.out.println("EDITIDDDDDDDDDD"+name);
                        System.out.println("EDITIDDDDDDDDDD"+no);
                        System.out.println("EDITIDDDDDDDDDD"+key);



                    /*ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

                    if ( (null == phoneCur)  ) {
        //                          createContact(name, phone);
                      } else {
                        ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                                .withSelection(where, params)
                                .withValue(ContactsContract.CommonDataKinds.Phone.DATA, no)
                                .build());
                    }

                    phoneCur.close();

                    try {
                        cr.applyBatch(ContactsContract.AUTHORITY, ops);
                    } catch (RemoteException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (OperationApplicationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
             */



                    id = null;
                    name = null;
                    no = null;
                    phoneCur = null;
                    key = null;

                contect_resolver = null;
                cur = null;

                             }
                    }
            }

        break;