Android : Adding Notes to Existing Contacts

两盒软妹~` 提交于 2019-12-22 08:11:18

问题


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();
            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 :(


回答1:


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".



来源:https://stackoverflow.com/questions/5327739/android-adding-notes-to-existing-contacts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!