安卓:添加注释到现有的联系人 [英] Android : Adding Notes to Existing Contacts

查看:111
本文介绍了安卓:添加注释到现有的联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ContactsContract API来一些笔记添加到我的联系人。我不知道我完全了解各种接触ID和生联系的ID。我的问题似乎类似于讨论pretty的<一个href="http://stackoverflow.com/questions/3100506/add-datas-phone-number-notes-etc-to-an-existing-contact-android-2-1">here.

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.

我想: 1.找到一个特定的联系人。 2.当发现,插入特定的笔记

I want to : 1. Find a specific contact. 2. When found, insert specific notes

我做它以下列方式:

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

推荐答案

替换

getContentResolver().update(...)

getContentResolver().insert( Data.CONTENT_URI, contentValues ); 

你也应该有一个WHERE语句中Data.CONTENT_URI查询。由于数据表中可以有相同的raw_contact_id多个条目。您可以使用Data.MIMETYPE = StructuredName.CONTENT_ITEM_TYPE。

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

这篇关于安卓:添加注释到现有的联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆