Search code examples
javaandroidautocompletetextview

Reverse AutoCompleteTextView suggestions order


I am using AutoCompleteTextView to suggest items to user when typing.

Because there is no space under the textView, it shows the suggestions above the textView. That's fine and I want to keep it that way.

However, the top suggestion is still on top of the suggestion list and I would like to reverse it so it's closer to the textView.

There is an image so you can understand better what I mean.

Sample

I would like to reverse the suggestions so "Item 1" is at the bottom etc.

With implementing the AutoCompleteTextSuggestion, I followed this tutorial.

This is the code I am using right now:

int layoutItemId = android.R.layout.simple_dropdown_item_1line;
String[] arr = getResources().getStringArray(R.array.items);
ArrayAdapter<String> adpt = new ArrayAdapter<>(this,layoutItemId,arr);

AutoCompleteTextView autoCompleteView = (AutoCompleteTextView) findViewById(R.id.autoCompleteText);
autoCompleteView.setAdapter(adpt);

I keep the suggestions in string array in res/values/strings.

Any help would be really appreciated. Thank you!


Solution

  • Perhaps you could reverse the data array itself before creating the adapter.

    You can achieve that by using the Collections.reverse(List<T>) method. Something like the example below:

    // Use the Collections.reverse() to reverse your list.
    ArrayList<String> myList = new ArrayList<>(Arrays.asList(arr));
    Collections.reverse(myList);
    // Now, create the array back again from the ArrayList.
    arr = myList.toArray(new String[0]);
    // Create the adapter.
    ArrayAdapter<String> adpt = new ArrayAdapter<>(this,layoutItemId,arr);
    

    Or... Since you're reading from a resource file to begin with... you could also write the info in reverse order.