我有一个问题。
在我的应用程序中,最终用户将输入uername,email和city。 现在,如果用户输入的电子邮件存在,则应运行更新查询。字段用户名和城市应该在他的手机中更新。如果电子邮件不存在,则应创建新的联系人。
现在我可以创建一个新条目,但不知何故更新查询无法正常工作。 可能是我在犯错误。
需要帮助。
我发布了我的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();
}
答案 0 :(得分:0)
//insert or update
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.NAME, fullname);
intent.putExtra(ContactsContract.Intents.Insert.PHONE,phnumber);
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
intent.putExtra(ContactsContract.Intents.Insert.NOTES,
"Imported from moodle");
intent.putExtra(ContactsContract.Intents.Insert.POSTAL, city
+ " " + country);
startActivityForResult(intent, 1);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 1:
if (requestCode == 1) {
}
super.onActivityResult(requestCode, resultCode, data);
}
}
}
答案 1 :(得分:-1)
这里放置更新方法的代码,根据您的要求进行更改。
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_PH_NO, contact.getPhoneNumber());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}