我一直在开发一个联系人应用程序,它允许:通话、短信、删除等……和编辑联系人。当用户单击并按住联系人时,会显示上下文菜单(下图)。操作调用、短信、...已完成,但编辑未完成。请给我一些代码或建议。提前致谢
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.call:
//do something
String mPhoneNumber = "tel:" + getPhoneNumber(mRecordId);
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(mPhoneNumber));
startActivity(intent);
return true;
case R.id.message:
//do something
String mSmsNumber = getPhoneNumber(mRecordId);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + mSmsNumber)));
return true;
case R.id.sendemail:
//do something
return true;
case R.id.edit:
//do something // need help???
return true;
case R.id.delete:
//do something
new AlertDialog.Builder(mContext).setMessage("Do you want to delete this contact?")
.setTitle("Delete").setCancelable(false).setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
deleteContactEntry(mRecordId);
populateContactList();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
}).show();
return true;
default:
return super.onContextItemSelected(item);
}
以下是我使用的方法:
/**
* Populate the contact list based on account currently selected in the account spinner.
*/
private void populateContactList() {
// Build adapter with contact entries
Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
mContactList.setAdapter(adapter);
}
/**
* Obtains the contact list for the currently selected account.
*
* @return A cursor for for accessing the contact list.
*/
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
(mShowInvisible ? "0" : "1") + "'";
// String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = 0";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
/**
* Launches the ContactAdder activity to add a new contact to the selected accont.
*/
protected void launchContactAdder() {
Intent i = new Intent(this, ContactAdder.class);
startActivity(i);
}
/**
* Get Phone Number
*/
private String getPhoneNumber(long contactId){
String mPhoneNumber = null;
String [] colums = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
String where = ContactsContract.Data.RAW_CONTACT_ID + "=?";
String[] whereParameters = new String[]{Long.toString(contactId)};
Cursor contacts = getContentResolver().query(ContactsContract.Data.CONTENT_URI,colums,where, whereParameters, null);
if (contacts.moveToFirst()) {
mPhoneNumber = contacts.getString(0);
}
contacts.close();
return mPhoneNumber;
}
/**
* Delete Contact
*/
private void deleteContactEntry(long contactId){
//String [] projection = new String [] {ContactsContract.RawContacts._ID};
String mSelectionClause = ContactsContract.RawContacts._ID + "=?";
String [] mSelectionArgs = new String [] {Long.toString(contactId)};
getContentResolver().delete(ContactsContract.RawContacts.CONTENT_URI, mSelectionClause, mSelectionArgs);
}