1

我正在尝试更改联系人的备注部分,我得到了他们的电话号码(receivedLocationSender),并且日志输出提供了正确的姓名和 ID,但我不知道如何让它替换联系人的“备注”部分。我目前所拥有的绝对没有任何作用。

       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();
                }
        }
4

1 回答 1

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();
于 2012-02-29T04:16:16.930 回答