1

嗨,我的应用程序中有一个 autocompletetextview。我正在获取存储在手机中的联系人。问题是我只能从 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);
        }
    }
4

1 回答 1

2

不用编写使用 选择多个联系人的逻辑ListView,我们可以简单地使用MultiAutoCompleteTextView。代码与 相同AutoCompleteTextView。通过这个我们可以选择多个联系人。

于 2012-08-10T11:33:51.077 回答