自定义 AutoCompleteTextView 行为
开箱即用的 AutoCompleteTextView 小部件似乎无法匹配列表值中间的输入字符串 - 匹配始终在开头进行;例如,输入“ar
”匹配“argentina
”,但不匹配“hungary
”。
如何搜索单词中间的文本?谁能给我一个主意吗?
提前致谢 !
Out of the box, the AutoCompleteTextView
widget does not seem to be able to match the input string in the middle of a list value - the matches are always made at the beginning; e.g., entering "ar
" matches "argentina
", but not "hungary
".
How can I search for the text in the middle of the word ? Can anyone give me an idea ?
Thanks in advance !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
}
}
我的建议是将字符串解析为字符数组。
然后迭代每个字符,直到找到字符串。
例如,假设您的搜索想要返回所有包含“ate”的单词,并且单词列表是...
状态
特征
斥责
晚了
你的算法应该是这样的
获取字符串并将其解析为字符数组
循环遍历数组并找到第一个“正确字符”(在我们的示例中为“a”)
找到该字符后,检查下一个字符,继续检查每个字符是否匹配,直到搜索的值完成。如果该字符不匹配,则退出数组迭代并转到下一个单词。
My advice would be to parse the string into a character array.
Then iterate each character until the string is found.
For example lets say your search wanted to return all words with "ate" in them and the word list was...
state
trait
berate
late
Your algorithm should go something like this
Take the string and parse it into a character array
Loop through the array and find the first "correct character" (in our example its 'a')
Once that character is found check the next character, keep checking each character for a match until the value being searched for is complete. If the character is not a match exit the array iteration and go to the next word.
您需要编写一个自定义
Filter
类并实现performFiltering
方法。此方法采用CharSequence
参数,您可以使用该参数执行所需的任何字符串操作,以便从数据集中生成匹配列表(在您的情况下,您可以使用String.contains
而不是String.startsWith
)。performFiltering
函数不在 UI 线程上运行。然后,您将匹配列表作为
FilterResults
返回 对象,其中包含一个Object
values(您的匹配列表,可能是一个ArrayList
)和一个int count 这是你的大小匹配列表。
最后,实现
publishResults
回调方法,一旦工作线程生成匹配列表,该方法就会返回,允许您调用notifyDataSetChanged
AutoCompleteTextView 的适配器,以便它可以显示结果。You would need to write a custom
Filter
class and implement theperformFiltering
method yourself. This method takes aCharSequence
argument, which you can use to perform whatever String operations you need in order to generate a list of matches from your dataset (in your case, you could useString.contains
instead ofString.startsWith
). TheperformFiltering
function is not run on the UI thread.You then return your list of matches as a
FilterResults
object, which contains anObject
values (your list of matches, probably anArrayList
) and anint
count which is the size of your list of matches.Finally, implement the
publishResults
callback method, which returns once the worker thread has generated the list of matches, allowing you to callnotifyDataSetChanged
on your AutoCompleteTextView's adapter so that it can display the results.老问题,但仍然相关。按照其他几个问题的指导,使用可过滤实现了自定义适配器。我制作了一个简单的通用适配器,可以使用 contains 进行搜索。关于它的快速说明:
我正在使用 butterknife,但是使用 findviewbyid 可以轻松完成 viewHolder。
布局 R.layout.list_item_simple 是带有文本视图 R.id.text_view_simple 的简单布局。
该对象需要一个要进行比较的 toString。
Old question, but still relevant. Following the guidance of a few other questions implemented a custom adapter using filterable. I made a simple generic adapter that searches with contains. Quick notes on it:
I'm using butterknife, but easy to do the viewHolder with findviewbyid.
The layout R.layout.list_item_simple is a simple layout with the textview R.id.text_view_simple.
The object needs a toString that will be compared.