Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select multiple contact in autocompletetextview

Hi i have a autocompletetextview in my application.In that iam fetching the contacts stored in the mobile.The problem is i can select only one contact from the autocompletetextview,But i want to select multiple contacts and have to show it in autocompletetextview like the messaging app in android.How to do this?

Iam using the following code:

@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);
        }
    }
like image 802
Manikandan Avatar asked Dec 13 '25 16:12

Manikandan


1 Answers

Instead of writing a logic for selecting multiple contacts using ListView, we can simply use MultiAutoCompleteTextView.The code is as same as for the AutoCompleteTextView.Through this we can select multiple contacts.

like image 193
Manikandan Avatar answered Dec 16 '25 06:12

Manikandan