opc0de
opc0de

Reputation: 11768

autocompletetextview doesn't suggest what I want

I am loading around 20,000 strings from a xml, besides that it takes very much time until the app really makes me a suggestion, when I type Cra it shows me the first suggestion Valea Crabului and i have Craiova in the strings but that is suggested later.

How can a AutoCompleteTextView suggest me only the words that match the whole word ?

Upvotes: 1

Views: 1617

Answers (1)

biegleux
biegleux

Reputation: 13247

If you are using ArrayAdapter for your AutoCompleteTextView, than here you can see default implementation of the filter for ArrayAdapter https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/ArrayAdapter.java

From the inner class ArrayFilter of ArrayAdapter:

for (int i = 0; i < count; i++) {
                final T value = values.get(i);
                final String valueText = value.toString().toLowerCase();

                // First match against the whole, non-splitted value
                if (valueText.startsWith(prefixString)) {
                    newValues.add(value);
                } else {
                    final String[] words = valueText.split(" ");
                    final int wordCount = words.length;

                    // Start at index 0, in case valueText starts with space(s)
                    for (int k = 0; k < wordCount; k++) {
                        if (words[k].startsWith(prefixString)) {
                            newValues.add(value);
                            break;
                        }
                    }
                }
            }

You see filter doesn't sort matched items by relevance you require, you have to write your own filter for your adapter.

Instead

                // First match against the whole, non-splitted value
                if (valueText.startsWith(prefixString)) {
                    newValues.add(value);
                } else {

you may need to use

                // First match against the whole, non-splitted value
                if (valueText.startsWith(prefixString)) {
                    newValues.add(0, value);
                } else {

so your filter will add values starting with your suggested string at the top of results as the most relevant filter result.

Upvotes: 3

Related Questions