Question

I'm trying to change the notes section of a contact, I got their phone number (receivedLocationSender) and the Log output gives the correct Name and ID, but idk how to get it to replace the "NOTES" section of the contact.. what I currently have does absolutely nothing.

       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();
                }
        }
Was it helpful?

Solution

After you have a contact id (id) from phonelookup.. the stuff below works

           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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top