Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

android - How to Edit contact from context Menu

I have been developing a contact app, it allows : call, sms, delele, etc ... and Edit Contact. When user click and hold on a contact, a context menu show (picture below). The actions call, sms, ... was completed but Edit is n't. Please give me some code or advice . Thanks in advance

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

Here are methods i used:

/**
 * 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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For Edit function you can use Edit intent to call default contact app

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

For sending Email have a look at this question or this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...