Peter Peter - 2 months ago 24
Android Question

AutoCompleteTextView with SimpleCursorAdapter for Contacts not returning cursor actual value

I have a AutoCompleteTextView that uses a SimpleCursorAdapter to filter emails for an input field. I have it working, although there are some

depreciated
commands that I am not sure how to rework.

The only problem I am having is when I select a value from the list provided, I am not getting the email address selected, but something like the following:

android.content.ContentResolver$CursorWrapperInner@13a08d9c

Here is the code I have:

final AutoCompleteTextView edt_Contact = (AutoCompleteTextView)findViewById(idTo);

ContentResolver cr = getContentResolver();
String[] projection={ContactsContract.CommonDataKinds.Email._ID,ContactsContract.CommonDataKinds.Email.ADDRESS};
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, null, null, null);

startManagingCursor(cursor);
String[] from = new String[] { ContactsContract.CommonDataKinds.Email.ADDRESS};
int[] to = new int[] { android.R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to);

adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[] {},
ContactsContract.CommonDataKinds.Email.ADDRESS + " LIKE '%" + constraint + "%'",
null, null);
}
});

edt_Contact.setAdapter(adapter);


Any suggestions on how to get the actual selected value to populate the AutoCompleteTextView when selected?

Also, as mentioned, the depreciated items are
startManagingCursor
and
SimpleCursorAdapter
.

Answer

Found the solution and I am posting it here for anyone else with a similar situation

I needed to add the following

adapter.setStringConversion(1);

I added it right before the last line in the example above. This changed the result from the

android.content.ContentResolver$CursorWrapperInner@13a08d9c

to the selected email address.

Just a heads-up too for anyone using the MultiAutoCompleteTextView to do the same thing, this code works for that as well... just change add Multi before the AutoCompleteTextView and add the .setTokenizer of your choice.