1

I got a AutoCompleteTextView which gets linked to a standard SimpleAdapter. The adapter loads autocomplete values from a ArrayList<Map<String,String>> called CITIES.

Now, what happens is that when I start typing the AutoCompleteTextView correctly shows me all the entries which include the part of text I already wrote. But what I'd like to get is to see on top of the dropdown list all the items which start with the text I wrote and, after that, all the other items in, say, A-Z order. This is because if you're looking for hotels in Venice you want for sure to see VENICE at the top of the list.

enter image description here

I know this is mainly a data problem and we should not retrieve city names in such a ugly fashion, but this is what we do have now. So my question is: is there any way to tell the AutoCompleteTextView in which order it should show the items in the dropdown list?

Thank you

Marco Zanetti
  • 4,051
  • 6
  • 32
  • 48

1 Answers1

0

You will have to write a custom sorting function in Android. That way you can control the way elements are sorted inside of a list. See this answer to see how to write a custom sorting function: Android java custom sorting using Comparable

Community
  • 1
  • 1
Alex van den Hoogen
  • 744
  • 1
  • 5
  • 22
  • Hello Alex, I got what you mean and I'd like to folllow your suggestion, but I got lost. What I understand is that I should implement my list as a ArrayList> where SortableMap implements Map and Comparator and SortableCitiesString implements Comparable. Is it right? Is there a simpler way? Thank you. – Marco Zanetti Mar 14 '14 at 12:06
  • Actually I think you are overthinking it with your Map inside of a List, although I don't know the exact application obviously. But what I would do is create an List which implements the Comparable interface and attach that to the AutoCompleteTextView. References: http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/ and http://developer.android.com/reference/android/widget/AutoCompleteTextView.html -- Good luck! – Alex van den Hoogen Mar 14 '14 at 13:02
  • Thank you Alex! Another question: what I need is to compare the elements of my dropdown list with the text I've been writing in the AutoCompleteTextView. This because if I wrote "ven" I want Venice to come up before "Avendics", because the string "ven" appears in the first three positions (0,1,2) of the word Venice and in position (1,2,3) in the word Avendics. Do you think this is possible? – Marco Zanetti Mar 14 '14 at 13:17
  • 1
    You will have to override the ArrayFilter (in ArrayAdapter) from the AutoCompleteTextView. Answer on this: http://stackoverflow.com/questions/11574752/autocompletetextview-doesnt-suggest-what-i-want – Alex van den Hoogen Mar 14 '14 at 13:35
  • Great, combining the sources you've pointed out I can see I got what I needed. I had to re-write SimpleAdapter (not ArrayAdapter) since I needed the Map, but your idea worked. – Marco Zanetti Mar 19 '14 at 15:50