0

I need to create a MultiAutoCompleteTextView with the phone numbers of the contacts on a user's device. What I need is similar to gmail; except with gmail email addresses are used. For the contacts, I have the following needs:

  • each phone number must be an entry. So if John has 3 numbers (home, cell, work), they show as 3 entries

  • each entry is searchable by phone number or by first/last name of person

To create my adapter, I try to modify the one provided by Google but when I download the sample, it does not compile (kind of a crappy experience when the thing is right out of the box, but I am trying troubleshoot it). Then using the sample at http://developer.android.com/reference/android/widget/MultiAutoCompleteTextView.html I will bound my MultiAutoCompleteTextView to the adapter. At this point, I am not sure how to convert the adapter to match my needs (i.e. search contacts by name or phone and to retrieve the numbers). So my call for help is this: has anyone successfully done this and don't mind sharing their code? Or Does anyone know how I can modify the linked adapter to give me phone numbers, which I can search by name or phone? And third, will the adapter work with MultiAutoCompleteTextView?

Note

In asking this question, I have made certain assumptions on how Google is implementing their MultiAutoCompleteTextView for emails. Does anyone know if that code is open source? Does anyone know if my assumptions are correct? Will my idea for implementing my contact phone MultiAutoCompleteTextView work?

UPDATE

So I have come a long way since asking the question. I am now using the answer at AutoComplete with name and number as in native sms app Android . But I am trying to convert the implementation to MultiAutoCompleteTextView but it's not allowing for multiple entries. Does anyone know how I might finish this?

UPDATE 2

Refer to AutoComplete with name and number as in native sms app Android :

My MultiAutoCompleteTextView is presently kind of working: it's allowing for multiple entries. I simply replaced AutoCompleteTextView with MultiAutoCompleteTextView, and I ignored the other answer's onItemClick suggestion. It's working, kind of. Except, the data I get is not the nice formatted elements that you see in the gmail EditText. So back to the original question: how is Google doing it? I don't want to spend time explaining how the gmail compose editText looks as the relevant reader can readily verify this. In their EditText I can enter four contacts and then with random access click on one to delete it. I want to be able to do that. How?

Community
  • 1
  • 1
learner
  • 11,490
  • 26
  • 97
  • 169
  • start from my answer here http://stackoverflow.com/questions/19858843/how-to-dynamically-add-suggestions-to-autocompletetextview-with-preserving-chara, it uses ACTV not MACTV but adapter should be the same – pskink Mar 16 '14 at 22:33
  • @pskink do I understand correctly? You are saying I should use your `SimpleCursorAdapter`? In that case, how would I modify the `FilterQueryProvider` so that I get phone and name and image of contacts (i.e. `QuickContactBadge`s?)? Do you mind modifying the previous answer here so the `provider` gets QuickContactBadge or whatever is appropriate. Thanks. – learner Mar 16 '14 at 22:58
  • just use ContentResolver.query with valid Uris to search Contacts by name or tel no – pskink Mar 16 '14 at 23:08
  • @pskink I have updated my answer to include the adapter I am trying to use. Presently it is not working. Any tips on how to fix it? – learner Mar 17 '14 at 17:44
  • So I have come a long way since asking the question. I am now using the answer at http://stackoverflow.com/questions/11147311/autocomplete-with-name-and-number-as-in-native-sms-app-android?rq=1 . I am trying to convert it to MultiAutoCompleteTextView but it's not allowing for multiple entries. – learner Mar 17 '14 at 19:16
  • you need some Uris like Contacts.CONTENT_FILTER_URI and Phone.CONTENT_FILTER_URI – pskink Mar 17 '14 at 19:24
  • actually only Phone.CONTENT_FILTER_URI is important – pskink Mar 17 '14 at 20:30
  • @pskink, the new code is working (revised OP), but it's set for only one entry. I need to have multiple entries. So I don't believe `Phone.CONTENT_FILTER_URI` is there answer. But if it is, where would I apply it? Feel free to use a code snippet. thanks. – learner Mar 17 '14 at 20:38
  • your code snippet is too complex to modify it – pskink Mar 17 '14 at 20:46
  • 1
    I don't see why this would be a secret. Google should not be hiding how they are managing to place views inside the `EditText`. It's a nice trick to be able to pull off. – Katedral Pillon Mar 18 '14 at 03:44
  • @learner have you seen my answer ? – pskink Mar 19 '14 at 05:51

1 Answers1

2

try this:

final Resources res = getResources();
LinearLayout ll = new LinearLayout(this);
AutoCompleteTextView tv = new AutoCompleteTextView(this);
tv.setThreshold(1);
String[] from = { Phone.DISPLAY_NAME };
int[] to = { android.R.id.text1 };
SimpleCursorAdapter a = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, from, to, 0);
a.setStringConversionColumn(2); // Phone.NUMBER
ViewBinder viewBinder = new ViewBinder() {
    @Override
    public boolean setViewValue(View v, Cursor c, int index) {
        TextView tv = (TextView) v;
        int typeInt = c.getInt(3); // Phone.TYPE
        CharSequence type = Phone.getTypeLabel(res, typeInt, null);
        // Phone.DISPLAY_NAME + Phone.NUMBER + type
        tv.setSingleLine(false);
        tv.setText(c.getString(1) + "\n" + c.getString(2) + " " + type);
        return true;
    }
};
a.setViewBinder(viewBinder);
FilterQueryProvider provider = new FilterQueryProvider() {
    @Override
    public Cursor runQuery(CharSequence constraint) {
        // run in the background thread
        Log.d(TAG, "runQuery constraint: " + constraint);
        if (constraint == null) {
            return null;
        }
        ContentResolver cr = getContentResolver();
        Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, constraint.toString());
        String[] proj = { BaseColumns._ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE, };
        return cr.query(uri, proj, null, null, null);
    }
};
a.setFilterQueryProvider(provider);
tv.setAdapter(a);
ll.addView(tv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
setContentView(ll);
pskink
  • 23,874
  • 6
  • 66
  • 77