autocompletetextview doesn't suggest what I want

ぐ巨炮叔叔 提交于 2019-12-23 02:54:23

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/11574752/autocompletetextview-doesnt-suggest-what-i-want

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!