Android : Adding Notes to Existing Contacts
I'm trying to use the ContactsContract api to add some notes to my contacts. I'm not sure I fully understand the various contact IDs and Raw Contact IDs. My problem seems pretty similar to what is discussed here.
I want to : 1. Find a specific contact. 2. When found, insert specific notes
I'm doing it in the following way:
Cursor contacts = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
while(contacts.moveToNext()){
Log.d("TC", "Found : " + name);
int rid = contacts.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID);
int rawContactID = contacts.getInt(rid);
int nameIdx = contacts.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
String name = contacts.getString(nameIdx);
if <Some condition>{
ContentValues contentValues = new ContentValues();
开发者_如何学JAVA contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactID);
contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.Note.NOTE, "Bazzinga !");
int stat = getContentResolver().update(ContactsContract.Contacts.CONTENT_URI, contentValues, null , null);
Log.d("TC", "Update returned : " + stat);
}
}
I get no errors. But the contact doesn't get updated :(
Replace the
getContentResolver().update(...)
with
getContentResolver().insert( Data.CONTENT_URI, contentValues );
Also you should have a where statement in the Data.CONTENT_URI query. Because Data table can have multiple entries with same raw_contact_id. You can use "Data.MIMETYPE = StructuredName.CONTENT_ITEM_TYPE".
精彩评论