如何在autocompletetextview中选择多个联系人

时间:2012-08-10 06:56:59

标签: android autocompletetextview

嗨我在我的应用程序中有一个autocompletetextview。在iam中获取存储在移动设备中的联系人。问题是我只能从autocompletetextview中选择一个联系人,但我想选择多个联系人并且必须在autocompletetextview中显示它像android中的消息应用程序。如何做到这一点?

我使用以下代码:

@Override
    public void afterTextChanged(Editable arg0) {

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {

    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

        Cursor c = getContentResolver().query(Phone.CONTENT_URI,
                PHONE_PROJECTION, null, null, null);
        startManagingCursor(c);

        ContactListAdapter adapter = new ContactListAdapter(this, R.layout.row,
                c,
                new String[] { Phone.DISPLAY_NAME, Phone.TYPE, Phone.NUMBER },
                new int[] { R.id.textName, R.id.textType, R.id.textPhone });

        adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

            @Override
            public boolean setViewValue(View view, Cursor cursor,
                    int columnIndex) {
                if (columnIndex != 2) {
                    return false;
                }

                int type = cursor.getInt(2);

                if (type == Phone.TYPE_CUSTOM) {
                    cursor.getString(3);
                }

                String text = (String) Phone.getTypeLabel(getResources(), type,
                        null);

                ((TextView) view).setText(text);

                return true;
            }
        });
        autoTo.setAdapter(adapter);
    }

    private static class ContactListAdapter extends SimpleCursorAdapter  {

        public static ContentResolver mContent;

        public ContactListAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to) {

            super(context, layout, c, from, to);
            mContent = context.getContentResolver();
        }

        @Override
        public CharSequence convertToString(Cursor cursor) {
            int namCol = cursor.getColumnIndexOrThrow(Phone.DISPLAY_NAME);
            int numCol = cursor.getColumnIndex(Phone.NUMBER);

            return cursor.getString(namCol) + " <" + cursor.getString(numCol)
                    + ">";
        }

        @Override
        public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
            FilterQueryProvider filter = getFilterQueryProvider();

            if (filter != null) {
                return filter.runQuery(constraint);
            }

            Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI,
                    Uri.encode(constraint.toString()));
            return mContent.query(uri, PHONE_PROJECTION, null, null, null);
        }
    }

1 个答案:

答案 0 :(得分:2)

我们可以简单地使用MultipleAutoCompleteTextView而不是编写用于使用listview选择多个联系人的逻辑。代码与autocompletetextview的代码相同。通过此我们可以选择多个联系人。

谢谢