我无法更新联系人的备注部分

时间:2012-02-26 01:06:08

标签: android contacts android-contentresolver

我正在尝试更改联系人的备注部分,我得到了他们的电话号码(receivedLocationSender)并且日志输出提供了正确的名称和ID,但idk如何让它替换“NOTES”部分的联系..我现在所做的一切都没有。

       private void displayContacts() {
               ContentResolver contentResolver = getBaseContext().getContentResolver();
               ContentValues contentValues = new ContentValues();
               Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(receivedLocationSender));

               String[] projection = new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID};

               Cursor cursor = contentResolver.query(
                       uri, 
                       projection, 
                       null, 
                       null, 
                       null);

               if(cursor!=null) {
                 while(cursor.moveToNext()){
                   String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
                   String contactId = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
                   contentValues.clear();
                   String noteWhereParams = ContactsContract.CommonDataKinds.Note.NOTE; 
                   String[] args = new String[] { String.valueOf(receivedLocation) };

                   contentValues.put(ContactsContract.CommonDataKinds.Note.NOTE, receivedLocation);
                   getContentResolver().update(ContactsContract.Data.CONTENT_URI, contentValues, contactId + "=?", args);

                   Log.d(LOGTAG, "contactMatch name: " + contactName);
                   Log.d(LOGTAG, "contactMatch id: " + contactId);
                   Log.d(LOGTAG, "contactNotes : " + ContactsContract.CommonDataKinds.Note.NOTE.toString());
                 }
                 cursor.close();
                }
        }

1 个答案:

答案 0 :(得分:1)

从phonelookup获得联系人ID(id)后,下面的内容可以使用

           ContentResolver cr = this.getContentResolver();
           ContentValues values = new ContentValues();

           values.clear();
           String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
           String[] noteWhereParams = new String[]{id,ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE}; 
           values.put(CommonDataKinds.Note.NOTE, "NEW NOTE HERE!!!!");

           cr.update(ContactsContract.Data.CONTENT_URI, values, noteWhere, noteWhereParams);

            Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
            if (noteCur.moveToFirst()) { 
                String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
                   Log.d(LOGTAG, "notes : " + note);
            } 
            noteCur.close();