简体   繁体   English

我想根据ID号将信息添加到联系人中,但是总是将其添加到错误的名称中。

[英]I want to add information into the contact according to the ID number, but it always being added to the wrong name.

I want to add a telephone number into the contact according to the ID number, but it always being added to the wrong name. 我想根据ID号将电话号码添加到联系人中,但始终将其添加到错误的名称中。 Can anyone tell me the reason why this happened. 谁能告诉我发生这种情况的原因。 It runs well in virtual device and sony mobile phone , but it come to an error as I said above in a new Samsung moble phone. 它可以在虚拟设备和索尼手机上很好地运行,但是如我上面在新的三星移动电话中所说,它出现了错误。 I can clearly confirm that the ID number is right These is the source code: 我可以清楚地确认ID号正确无误这些是源代码:

ContentValues values = new ContentValues (); 
values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, contactID);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, shortNumber);
values.put(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_OTHER);
getContentResolver().insert(Data.CONTENT_URI,values); 
values.clear();

The answer here may help: Inserting contacts in Android 2.2 . 此处的答案可能会有所帮助: 在Android 2.2中插入联系人

It seems the code above does not work on all devices as per the comments there. 根据上面的注释,似乎上面的代码不适用于所有设备。 The answer posted by Alok Save https://stackoverflow.com/users/452307/alok-save , suggests using the applyBatch() method as follows: Alok Save发布的答案https://stackoverflow.com/users/452307/alok-sa​​ve ,建议如下使用applyBatch()方法:

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
   .withValue(RawContacts.ACCOUNT_TYPE, null)
   .withValue(RawContacts.ACCOUNT_NAME,null )
   .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
   .withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
   .withValue(Phone.NUMBER, "9X-XXXXXXXXX")
   .build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
   .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
   .withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
   .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
   .build());  

ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想显示联系人姓名和电话号码,但失败 - I want to display contact name and phone number but it fails 我想获取联系人姓名和电话号码,但是我的代码只是打印联系人姓名,所以我怎么也可以获取电话号码 - I want to get the contact name and number but my code just prints the contact name so how can I also get the number 无法使用联系人ID检索姓名和电话号码 - Unable to retrieve name and number using contact ID 如何从联系人列表中获取电子邮件ID,姓名和电话号码 - How to get email id, name and number from contact list 我正在尝试在我的 recyler 视图联系人列表中添加姓名和号码,但是当我单击添加按钮时,应用程序停止工作并关闭 - i am trying to add name and number in my recyler view contact list but when i click add button the app stop working and close 我想在我的 sql 数据库中添加一个搜索,function 通过 id 获取名称会创建一个错误 - I want to add a search in my sql database, function to get the name by the id creates an error 我的gwt应用程序包含没有ID,名称的组件。 有什么方法可以为组件设置ID /名称(运行时),以便将它们用于硒测试吗? - My gwt application have components without ID,name. Is any way to set id/name (runtime) to components so i use them for testing in selenium? Card类有什么问题,我不希望它是抽象的 - What is wrong with my Card class, I do not want it being abstract 尝试检测具有相同名字和姓氏的人时,我的ArrayList有什么问题。 我正在使用正则表达式模式 - What is wrong with my ArrayList when trying to detect persons who have same first name and last name. I am using Regex Pattern 当我向其中添加新的电话号码时,Android联系人消失 - Android contact disappears when I add new phone number to it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM