我有一個問題。如何使用ContactsContract更新所有字段
在我的應用程序中,最終用戶將輸入用戶名,電子郵件和城市。 現在,如果用戶輸入的電子郵件存在,則應運行更新查詢。用戶名和城市應該在他的手機中更新。如果電子郵件不存在,則應該創建一個新的聯繫人。
現在我能夠創建一個新的條目,但不知何故更新查詢無法正常工作。 可能是我犯了一個錯誤。
需要幫助。
我發佈我的createContact和updateContact代碼。創建聯繫人正常工作。它可以插入一個新的聯繫人。
//創建聯繫人
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
//------------------------------------------------------ Names
if(!DisplayName.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, DisplayName)
.build());
}
if(emailID != null) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, emailID)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
}
if(!street.equals("") || !city.equals("") || !state.equals("") || !zipcode.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, street)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, city)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, zipcode)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, state)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
.build());
}
//更新聯繫
public void updateEmailContact(String fname, String email) {
Cursor cur = managedQuery(ContactsContract.Data.CONTENT_URI, null, null, null, null);
if ((null == cur) || (!cur.moveToFirst()));
Log.i("Test", "----------------------Cursor Count----------------------" + cur.getCount());
if(cur != null) {
if(cur.moveToFirst()) {
do {
String raw_contact_id = cur.getString(cur.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
String where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE) + " = ?";
String[] params = new String[]{raw_contact_id,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_WORK)};
Cursor emailCur = managedQuery(ContactsContract.Data.CONTENT_URI, null, where, params, null);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Log.i("Test", "--------Email Count---------------" + emailCur.getCount());
if ((null == emailCur) || (!emailCur.moveToFirst()))
{
}
else
{
String emailAddress = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String email_name = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
Log.i("Test", "------------------emailAddress----------------" + emailAddress + " " + email_name);
Log.i("Test", "-------------------fname----------------------" + fname_edit.getText().toString().trim());
if(emailAddress.equalsIgnoreCase(email)) {
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(CommonDataKinds.StructuredName.DISPLAY_NAME + "=?", new String[] {"Krishna"})
.withValue(CommonDataKinds.StructuredName.DISPLAY_NAME, fname_edit.getText().toString().trim())
.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//return true;
} else {
}
}
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Toast.makeText(getBaseContext(), "Contact Updated.....!!!!", Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
emailCur.close();
}while(cur.moveToNext());
}
}
cur.close();
}
我正在檢查您的代碼..但我需要檢查......電話號碼是否存在。如果電話號碼存在,那麼該條目應該更新 – 2012-03-06 11:44:03
我在哪裏更新我的方法錯了? – 2012-03-06 11:45:27
我不想這樣做意圖...... – 2012-03-06 11:54:38