[英]I am not able to update my email in android application using content provider
i am working on the application which retrieve all the android contacts and also can delete and update contacts.everything working fine problem is when contact does not have email address then i can not update it using my application (can not add email address from my update contact page). 我正在处理可检索所有android联系人的应用程序,并且还可以删除和更新contacts.everything一切正常的问题是当联系人没有电子邮件地址时,我无法使用我的应用程序进行更新(无法从更新中添加电子邮件地址联系页面)。 i have written used code below. 我在下面写了二手代码。 thanks in advance. 提前致谢。
private void updateEmaill(String id, String email) {
ContentResolver cr = getContentResolver();
String where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND "
+ ContactsContract.Data.MIMETYPE + " = ?";
String[] params = new String[] { id,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE };
Cursor phoneCur = managedQuery(ContactsContract.Data.CONTENT_URI, null,
where, params, null);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
if ((null == phoneCur)) {
System.out.println("we have got null value from the cursor");
} else {
System.out.println("phone cursor count" + phoneCur.getCount());
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where, params)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS,
email).build());
}
phoneCur.close();
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
newUpdate() has been known to work only if a given field exists already. 仅当给定字段已经存在时,才知道newUpdate()可以工作。
As a workaround, here's one simple logic you can use: 解决方法是,可以使用以下一种简单逻辑:
-check if email exists for contacts
-if it does call update
-if it doesn't save all the fields of this contact in some variables and delete the contact
-use newInsert() to create a new contact with the saved fields from old contact and also the email that you need to add.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.