0

私は連絡先アプリを開発しており、通話、SMS、削除など...および連絡先の編集が可能です。ユーザーが連絡先をクリックしたままにすると、コンテキスト メニューが表示されます (下の図)。通話、SMS などのアクションは完了しましたが、編集は完了していません。コードやアドバイスを教えてください。前もって感謝します

画像はこちら

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

1 に答える 1

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

メールの送信については、この質問またはこれをご覧ください

于 2012-11-02T03:25:04.490 に答える