使用自动完成TextView从联系人获取电子邮件?

时间:2011-12-22 11:01:29

标签: android email android-contacts

在我的应用程序中,我有一个AutoComplete TextViewEditText在此AutoComplete TextView中提供了来自通讯录的所有联系人姓名。

我想获取在我的自动完成文本视图中选择哪个联系人的主电子邮件ID到editText。我怎样才能实现这一目标?有人指导我吗?

2 个答案:

答案 0 :(得分:2)

     public class Contact extends Activity {
    /** Called when the activity is first created. */
    private static final int CONTACT_PICKER_RESULT = 10;
    private static final String DEBUG_TAG = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void doLaunchContactPicker(View view) {
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
        startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case CONTACT_PICKER_RESULT:
                Cursor cursor = null;
                String email = "";
                try {
                    Uri result = data.getData();
                    Log.v(DEBUG_TAG, "Got a contact result: "
                            + result.toString());

                    // get the contact id from the Uri
                    String id = result.getLastPathSegment();

                    // query for everything email
                    cursor = getContentResolver().query(Email.CONTENT_URI,  null, Email.CONTACT_ID + "=?", new String[] { id }, null);

                    int emailIdx = cursor.getColumnIndex(Email.DATA);

                    // let's just get the first email
                    if (cursor.moveToFirst()) {
                        email = cursor.getString(emailIdx);
                        Log.v(DEBUG_TAG, "Got email: " + email);
                    } else {
                        Log.w(DEBUG_TAG, "No results");
                    }
                } catch (Exception e) {
                    Log.e(DEBUG_TAG, "Failed to get email data", e);
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                    EditText emailEntry = (EditText) findViewById(R.id.invite_email);
                    emailEntry.setText(email);
                    if (email.length() == 0) {
                        Toast.makeText(this, "No email found for contact.",
                                Toast.LENGTH_LONG).show();
                    }
                }
                break;
            }

        } else {
            Log.w(DEBUG_TAG, "Warning: activity result not ok");
        }
    }
}

答案 1 :(得分:1)

确保您拥有READ_CONTACTS您的应用的权限。

    AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
    Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);
    startManagingCursor(emailCursor);
    autoCompleteTextView.setAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, emailCursor, new String[] {Email.DATA1}, new int[] {android.R.id.text1}));
    autoCompleteTextView.setThreshold(0);

请注意AutoCompleteTextView区分大小写。