使用自动完成文本视图从联系人获取电子邮件?

发布于 2024-12-22 11:41:30 字数 206 浏览 1 评论 0原文

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

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

In my application i've one AutoComplete TextView and EditText In this AutoComplete TextView provides all the contact names from Contacts.

I want to get the primary email id which contact was selected in my AutoComplete Textview to the editText. How can i achieve this? Anyone guide me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念绕指尖 2024-12-29 11:41:30
     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");
        }
    }
}
     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");
        }
    }
}
妖妓 2024-12-29 11:41:30

确保您的应用拥有 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 区分大小写。

Make sure you have READ_CONTACTS permission for your App.

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

Please Note AutoCompleteTextView is Case Sensitive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文