android在自动完成textview中获取电子邮件建议

时间:2014-12-08 12:54:43

标签: android email autocompletetextview

我希望使用自动完整文本视图在我的联系人中收到电子邮件作为建议。 我目前正在获取联系人的姓名,联系人的身份和号码,如

@SuppressWarnings("deprecation")
    public void PopulatePeopleList() {

        mPeopleList.clear();

        try {

            Cursor phones = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    null, null, null);

            while (phones.moveToNext()) {

                String num = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                num = num.replaceAll(" ", "");
                String name = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String id = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

                Map<String, String> NamePhoneType = new HashMap<String, String>();

                NamePhoneType.put("Name", name);
                NamePhoneType.put("Phone", num);
                NamePhoneType.put("Type", id);
                mPeopleList.add(NamePhoneType);

                // Add ArrayList names to adapter

            }

        } catch (Exception e) {
            Log.i("AutocompleteContacts", "Exception : " + e);
        }

        // startManagingCursor(people);
    }

1 个答案:

答案 0 :(得分:3)

已经有一段时间了,但对于那些寻求同一问题解决方案的人,我写了EmailCompletionEditText,它可以给你一个完整的答案或自己编写解决方案的想法。

如果链接断开,我在这里附上源代码:

/**
 * Created by Rany Albeg Wein on 24/01/2016.
 */
public class EmailCompletionEditText extends MultiAutoCompleteTextView {
    private ContentResolver mContentResolver;

    public EmailCompletionEditText(Context context) {
        super(context);
        init(context);
    }

    public EmailCompletionEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public EmailCompletionEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public EmailCompletionEditText(Context context, AttributeSet attrs, int defStyleAttr,
                                   int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        mContentResolver = context.getContentResolver();

        setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

        final String[] from = new String[]{ContactsContract.Contacts.DISPLAY_NAME,
                                           ContactsContract.CommonDataKinds.Email.ADDRESS,
                                           ContactsContract.Contacts.Photo.PHOTO_URI};

        final int[] to = new int[]{R.id.tv_contact_name,
                                   R.id.tv_contact_email,
                                   R.id.iv_contact_photo};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, R.layout.contact_row, null, from, to, 0) {
            @Override
            public CharSequence convertToString(Cursor cursor) {

                final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS);
                return cursor.getString(emailIndex);
            }
        };

        adapter.setFilterQueryProvider(new FilterQueryProvider() {
            @Override
            public Cursor runQuery(CharSequence constraint) {
                if (constraint == null) {
                    return null;
                }

                String query = constraint.toString();

                final String selection = ContactsContract.Contacts.DISPLAY_NAME
                        + " LIKE ? "
                        + " OR "
                        + ContactsContract.CommonDataKinds.Email.ADDRESS
                        + " LIKE ? ";

                String[] selectionArgs = new String[]{"%" + query + "%"
                        , "%" + query + "%"};

                return mContentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, selection, selectionArgs, null);

            }
        });

        setAdapter(adapter);
    }
}