0

I am creating an AutoCompleteTextView field on a form that I want to possibly populate with a matching selection from the CONTACTS email addresses.

Doing some searching, I was able to find some older examples on here (that have alot of depreciated code... nothing newer) but with some trial and error I was able to get it to work matching the first characters of the email address as I type... although I don't have a 100% understanding of exactly how this works.

What I would really like to do is have it show ANY match ANYWHERE in the email addresses on file. That is, if I type gma, I would like it to show: gmartin@xyz.com, gregmaster@yahoo.com, jim@gmail.com, sam@gmail.com, etc.

As I understand it, I need to use a LIKE command in the query. But no matter how I format it, I get the same results... only matches the start of the email address.

Here is my current code attempt with the LIKE....

    ArrayList<String> emailAddressCollection = new ArrayList<String>();
    ContentResolver cr = getContentResolver();
    String[] projection={ContactsContract.CommonDataKinds.Email.DATA};
    Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, ContactsContract.CommonDataKinds.Email.ADDRESS + " LIKE '%A%'", null, null);
    while (emailCur.moveToNext())
    {
        String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        emailAddressCollection.add(email);
    }
    emailCur.close();
    String[] emailAddresses = new String[emailAddressCollection.size()];
    emailAddressCollection.toArray(emailAddresses);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, emailAddresses);

    AutoCompleteTextView actextView = (AutoCompleteTextView)findViewById(idTo);
    actextView.setAdapter(adapter);

Any idea what I am missing here?

Also, another somewhat related question, in my dropdownlist, i would like to show the email address match plus the NAME listed in that record... and just return the email address when selected.

Can this be accomplished using this code or do I need to look at something else?

Peter
  • 427
  • 4
  • 14

1 Answers1

0

AutoCompleteTextView is designed by default to match based ONLY on the starting characters. So the gma in sam@gmail.com will not be recognized. You can implement your own widget which does a lookup based on the 'LIKE' criteria. Android only searches through the first few characters for each entry in the adapter elements.

If you must choose to search by 'LIKE' criteria, it is possible although it would be quite a task in itself. You could simply search by whether the string contains the text and then choose to filter by that - no need to query everytime. Alternatively, you could try searching for a library which does this.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • no, AutoCompleteTextView is **NOT** `"designed by default to match based ONLY on the starting characters"`, matches only depend on the `Adapter` you are using (and more precisely on `Filter` that is used by that `Adapter`) – pskink Sep 15 '16 at 05:51
  • So is the problem I am having then and adapter issue, filter issue, or something else? I know this can be done, even the GMAIL app does it when you type any portion of an email address. – Peter Sep 16 '16 at 00:33
  • @Peter just use `SimpleCursorAdapter` and setup its `FilterQueryProvider` – pskink Sep 16 '16 at 05:46
  • I actually found an example, on StackOverFlow,... [link](http://stackoverflow.com/questions/6300508/how-to-filter-results-of-autocompletetextview)... but there is a lot of depreciated code referenced (I am designing for SDK 15+). I figured out how to "fix" some of it, but not all so I can't get it to work either – Peter Sep 16 '16 at 23:19
  • `"lot of"`? for example? – pskink Sep 17 '16 at 05:28
  • Sorry, I was out of town all week... Using the code from the link... the `startManagingCursor` shows depreciated as does the `SimpleCursorAdapter` itself. The `R.layout.simple_contact_textview` is showing a `can't be resolved`. And finally, I am also not sure how use/handle the `from` & `to` items needed for the SimpleCursorAdapter. Any suggestions? – Peter Sep 26 '16 at 02:32