我正在学习android。我正在尝试以编程方式更新联系号码。任何人都可以帮助我,我该怎么办呢。
我的努力是:
String lNumber = pCur.getString( pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
ContentValues values = new ContentValues();
Uri lPhoneUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, ContactsContract.CommonDataKinds.Phone.NUMBER);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "45323333"));
getContentResover().update(lPhoneUri, values, ContactsContract.CommonDataKinds.Phone.NUMBER+"=?", new String[] { lNumber });
答案 0 :(得分:14)
我认为你几乎就在那里。以下使用新API更新联系人的WORK电话号码,假设该联系人已有工作电话号码。
public void updateContact (String contactId, String newNumber, Activity act)
throws RemoteException, OperationApplicationException{
//ASSERT: @contactId alreay has a work phone number
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" +
Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Phone.TYPE + "=?";
String[] phoneArgs = new String[]{contactId, String.valueOf(Phone.TYPE_WORK)};
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(Phone.NUMBER, newNumber)
.build());
act.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
答案 1 :(得分:7)
试试这个
String where = ContactsContract.Data.DISPLAY_NAME + "=? AND " +
ContactsContract.Data.MIMETYPE + "=? AND " +
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE) + "=?";
String[] params = new String[] {
Cname,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
};
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where, params)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,"9999999999")
// .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Sample Name 21")
.build()
);
//email
String where3 = ContactsContract.Data.DISPLAY_NAME + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ?";
String[] params3 = new String[] {
Cname,
"vnd.android.cursor.item/email_v2"
};
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where3, params3)
.withValue(ContactsContract.CommonDataKinds.Email.DATA,"a@b.com")
.build()
);
通过使用此功能,我可以更新联系人详细信息。
答案 2 :(得分:3)
只需稍加更改,如果您需要更新除工作电话之外的其他电话,您可以使用将此作为参数的方法。
public void updateContact (String contactId, String newNumber, String phoneType)
throws RemoteException, OperationApplicationException{
//ASSERT: @contactId alreay has a work phone number
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" +
Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Phone.TYPE + "=?";
String[] phoneArgs = new String[]{contactId, phoneType};
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(selectPhone, phoneArgs)
.withValue(Phone.NUMBER, newNumber)
.build());
this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
答案 3 :(得分:1)
此课程可能有助于更新联系人。
public class UpdateContact extends Activity {
private EditText txtName,txtPh;
private Button btnUpdate;
private TextView txt;
private static final String TAG_ID="id";
private static final String TAG_NAME = "name";
private static final String TAG_PHNO = "phNo";
private static int id;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_contact);
txtName=(EditText)findViewById(R.id.txtNname);
txtPh=(EditText)findViewById(R.id.txtNPhNo);
btnUpdate=(Button)findViewById(R.id.btnUpdate);
txt=(TextView)findViewById(R.id.txtId);
Intent i=getIntent();
id=i.getIntExtra(TAG_ID,0);
txtName.setText(i.getStringExtra(TAG_NAME));
txtPh.setText(i.getStringExtra(TAG_PHNO));
//to update contact on button click you can apply this
btnUpdate.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
DatabaseHandler handler=new DatabaseHandler(UpdateContact.this);
Contact contact=new Contact();
contact.setID(id);
contact.setName(txtName.getText().toString());
contact.setPhNo(txtPh.getText().toString());
handler.UpdateContact(contact);
finish();
}
});
}
}
答案 4 :(得分:0)
这是您更新Phone.Home的方式。
val rawContactId = "101"
val contentProviderOperation = ArrayList<ContentProviderOperation>()
val where = ContactsContract.Data.RAW_CONTACT_ID + "=? AND " +
ContactsContract.Contacts.Data.MIMETYPE + "=" +
"'${ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE}'" + " AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + "=?"
val params = arrayOf(rawContactId, "${ContactsContract.CommonDataKinds.Phone.TYPE_HOME}")
contentProviderOperation.add(
ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where, params)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "+60123456789")
.build()
)
try {
contentResolver.applyBatch(ContactsContract.AUTHORITY, contentProviderOperation)
showToastyMessage("Contact Update Successfully")
} catch (exception: Exception) {
showToastyMessage(exception.message.toString())
}