Android – 新的数据记录被添加到错误的联系人

前端之家收集整理的这篇文章主要介绍了Android – 新的数据记录被添加到错误的联系人前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试添加数据记录到一个已经存在的联系人,我找到联系人使用电话查找,我采取联系_id字段,并添加一个新的数据与raw_contact_id设置为_id字段.
在某些联系人上,它不起作用,它将数据与不同的联系人相匹配.
(我认为它与存储在SIM卡上的联系人有关)

请咨询,也许你有一种不同的方法添加数据

代码示例:

@H_403_8@LinkedList<Long> lcv = new LinkedList<Long>(); ContentResolver cr = getContentResolver(); Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phoneNumber)); Cursor c = cr.query(uri,null,null); try { while (c.moveToNext()) { Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI,c.getString(c.getColumnIndex(PhoneLookup.LOOKUP_KEY))); Cursor c2 = getContentResolver().query(lookupUri,new String[] { Contacts._ID,Contacts.DISPLAY_NAME },null); try { if (c2.moveToNext()) { Log.i(TAG,"found: " + c2.getLong(c2.getColumnIndex(Contacts._ID)) + "," + c2.getString(c2.getColumnIndex(Contacts.DISPLAY_NAME))); lcv.add(c2.getLong(c2.getColumnIndex(Contacts._ID))); } else { Log.e(TAG,"Failed to lookup"); } } finally { c2.close(); } } } finally { c.close(); } for (Long rawid : lcv) { Cursor c3 = cr.query(RawContacts.CONTENT_URI,RawContacts.CONTACT_ID + "=?",new String[] {rawid+""},null); if (c3.moveToNext()) { Log.e(TAG,"aaaa: " + c3.getString(c3.getColumnIndex(Contacts.DISPLAY_NAME))); } else { Log.e(TAG,"errrrror"); } ContentValues cv = new ContentValues(); cv.put(Data.RAW_CONTACT_ID,rawid + ""); cv.put(Data.MIMETYPE,MyMime.MIMETYPE); cv.put(Data.DATA1,"mydata"); cv.put(Data.SYNC1,syncvalue); Uri newIns = cr.insert(ContactsContract.Data.CONTENT_URI,cv); Log.i(TAG,"insert: " + newIns + "," + name); }

解决方法

问题在于您选择Contacts._ID并使用此标识填充LinkedList lcv中的数据. @H_403_8@Cursor c2 = getContentResolver().query(lookupUri,null);

您实际上需要一个RAW_CONTACT_ID.

DISPLAY_NAME可以从Contacts数据库/ ContactsContract.Data’OR’数据库/ ContactsContract.CommonDataKinds.StructuredName’OR’database / RawContactsEntity中获取.在后两种情况下,您将能够使用RAW_CONTACT_ID获取DISPLAY_NAME

几个关键指针:

> Contacts._ID = Data.CONTACT_ID
> RawContacts._ID = Data.RAW_CONTACT_ID
> RawContacts.CONTACT_ID = Contacts._ID
> RawContactsEntity._ID = RawContacts._ID

听起来很混乱让我尝试…

>联系人数据库分为3个表联系人,原始联系人和数据.
>每个表包含列(_ID),它是自动递增的主键.
>数据表包含所有联系信息,如电话号码,邮件ID,地址等.
>原始联系人指向创建的实际联系人.因此,我们在添加联系人时使用原始联系人.
>用户无法在联系人表中添加任何数据.由于aggregation of contacts,此表中的数据在内部填充.
>您的逻辑为某些联系人工作的原因是:_ID为联系人,原始联系人保持相同,直到发生任何联系人聚合.让我们说你添加两个同名的联系人abc.这里,原始联系人的_ID会增加两倍,而联络人的_ID仅增加一次,因为这两个联系人由于aggregation of contacts而被合并

有关详细信息,请参阅this.

在您的案例中获取信息的最佳方法是使用ContactsContract.RawContactsEntity(raw_contacts表的外部连接与数据表)

参考:http://developer.android.com/reference/android/provider/ContactsContract.RawContactsEntity.html

猜你在找的Android相关文章