3

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);
        }
    }
Manikandan
  • 1,802
  • 5
  • 20
  • 28
  • I am doing something similiar HERE!!! http://stackoverflow.com/questions/12854336/autocompletetextview-backed-by-cursorloader – Etienne Lawlor Oct 30 '12 at 19:45

1 Answers1

3

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.

alexbt
  • 16,415
  • 6
  • 78
  • 87
Manikandan
  • 1,802
  • 5
  • 20
  • 28
  • Thanks! You mean: http://developer.android.com/reference/android/widget/MultiAutoCompleteTextView.html – bentzy Dec 01 '13 at 09:30