如何从上下文菜单编辑联系人

时间:2012-11-02 02:18:39

标签: android contextmenu

我一直在开发一个联系人应用程序,它允许:呼叫,短信,删除等...和编辑联系人。当用户点击并按住某个联系人时,会显示一个上下文菜单(如下图所示)。动作调用,短信,...已完成,但编辑不是。请给我一些代码或建议。提前致谢

Image is here

@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);
}

1 个答案:

答案 0 :(得分:0)

对于编辑功能,您可以使用编辑意图来调用默认联系人应用

Intent i = new Intent(Intent.ACTION_EDIT);
i.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + id)); //contact's id
startActivityForResult(i, idEDIT_CONTACT);

要发送电子邮件,请查看此questionthis