I have just implemented an autocompletion for a textfield using an online webservice, based on this answer on Stackoverflow:
ArrayAdapter is updated late from Webservice in AutoCompleteTextAdapter
Using an ArrayAdapter<User> implements Filterable, I have managed that the autocompletion suggests me entries as intended.
User is a Java Bean that contains information which is presented in the suggestion (age, name, …).
When I select a suggestion, the Autocomplete field is filled with the ‘wrong’ data – using the toString()method, instead of the ‘name’ property of the bean.
My Question is: Can I override (in the Adapter) a method which will allow me to specify how to convert the bean so that the correct property is returned for the AutoCompleteTextView?
(Ideally, User.toString() should not be changed)
Thx!
I have found another way:
The method
convertSelectionToString(Object selectedItem)in AutoCompleteTextView can be overridden by subclasses to allow for custom conversions. This way, no adjustment to thetoString()method is required.This – it seems to me – has the advantage that the Filter can return not just a list of Strings but a list of custom objects, which can be used by
getView(int position, View convertView, ViewGroup parent)of the adapter to construct “richer” suggestions.The obvious disadvantage is that it requires subclassing AutoCompleteTextView for every Filterresult type whose toString() method shall not be modified.
Any comments on this?