我想在我的应用程序中编辑设备主要用户的个人资料。因此,我使用ContentProviderOperation
和newUpdate
方法收集更改,然后使用ContentResolver
方法提交它们:applyBatch
。这是我到目前为止编写的代码:
public ContentProviderResult[] updateProfile(Contact userProfile) {
try {
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, Integer.valueOf(userProfile.id))
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, edtName.getText().toString().trim())
.build());
return getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
我得到任何运行时异常但没有提交更改! 我错过了什么吗?
答案 0 :(得分:0)
我已经弄明白了,问题是我不知道如何在ContactsContract.Data
表上检索用户档案public ContentProviderResult[] createOrUpdateUserProfile() {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
try {
ops.add(ContentProviderOperation.
newDelete(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
ops.clear();
//Create User Profile
ops.add(ContentProviderOperation.newInsert(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
//Create User Name
if (!(edtName.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, edtName.getText().toString())
.build());
}
if (!(edtHomePhone.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, edtHomePhone.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 1)
.build());
}
if (!(edtMobilePhone.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, edtMobilePhone.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 2)
.build());
}
if (!(edtWorkPhone.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, edtWorkPhone.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 3)
.build());
}
if (!(edtHomeEmail.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, edtHomeEmail.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 1) // contact.numbers.get(0).type
.build());
}
if (!(edtWorkEmail.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, edtWorkEmail.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 2) // contact.numbers.get(0).type
.build());
}
/*String completeAddress = edtStreet.getText().toString() + edtCity.getText().toString() + edtState.getText().toString()
+ edtCountry.getText().toString();*/
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, edtStreet.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, edtCity.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, edtState.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, edtCountry.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, edtZip.getText().toString())
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, 1)
.build());
if (!(edtSkype.getText().toString().matches(""))) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Im.DATA, edtSkype.getText().toString())
.withValue(ContactsContract.CommonDataKinds.Im.PROTOCOL, 3)
.build());
}
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
return res;
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
以进行投影。
我的快速修复是购买删除用户个人资料记录,然后创建一个新记录。这是我到目前为止使用的代码:
for p, v in zip(chart.series[0].points, dataSeries):
p.data_label.position = XL_LABEL_POSITION.ABOVE
p.data_label.text_frame.clear()
for paras in p.data_label.text_frame.paragraphs:
paras.font.size = Pt(10)
p.data_label.text_frame.text = str.format('{0:.1f}', v)
答案 1 :(得分:0)
你的代码有点错误。
public ContentProviderResult[] updateProfile(Contact userProfile) {
try {
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, Integer.valueOf(userProfile.id)) // Error is here
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, edtName.getText().toString().trim())
.build());
return getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
用于此行:
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, Integer.valueOf(userProfile.id))
这个
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, getRawContactId(id))
public static String getRawContactId(String contactId)
{
String res = "";
Uri uri = ContactsContract.RawContacts.CONTENT_URI;
String[] projection = new String[]{ContactsContract.RawContacts._ID};
String selection = ContactsContract.RawContacts.CONTACT_ID + " = ?";
String[] selectionArgs = new String[]{ contactId };
Cursor c = ContentManager.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if(c != null && c.moveToFirst())
{
res = c.getString(c.getColumnIndex(ContactsContract.RawContacts._ID));
c.close();
}
return res;
}