问题描述
我加载大约20,000串从XML,除了它需要很多的时间,直到应用程序真的让我一个建议,当我输入 CRA
它让我看到第一个建议的Valea Crabului
和我有克拉约瓦
中的字符串,但以后建议。
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.
如何能 AutoCompleteTextView
建议我只整个单词相匹配的话?
How can a AutoCompleteTextView
suggest me only the words that match the whole word ?
推荐答案
如果您使用的是 ArrayAdapter
您 AutoCompleteTextView
,比这里你可以看到 ArrayAdapter
https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/ArrayAdapter.java
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
从内部类 ArrayFilter
的 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.
而不是
// First match against the whole, non-splitted value
if (valueText.startsWith(prefixString)) {
newValues.add(value);
} else {
您可能需要使用
// 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.
这篇关于autocompletetextview并不意味着我想要什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!