自AutoCompleteTextView行为 [英] Custom AutoCompleteTextView behavior
问题描述
开箱即用,在 AutoCompleteTextView
部件似乎并不能够在输入字符串列表中的中间值相匹配 - 比赛始终在做开始;例如,输入 AR
匹配阿根廷
,而不是匈牙利
。
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 ?
在此先感谢!
推荐答案
您将需要编写一个自定义的 过滤器
类,并实现<一个href=\"http://developer.android.com/reference/android/widget/Filter.html#performFiltering%28java.lang.CharSequence%29\"相对=nofollow> performFiltering
方法自己。此方法需要一个的CharSequence
参数,你可以用它来执行你为了从数据集生成匹配的列表需要的任何字符串操作(在你的情况,你可以使用 String.contains
而不是 String.startsWith
)。在 performFiltering
函数不是在UI线程上运行。
You would need to write a custom Filter
class and implement the performFiltering
method yourself. This method takes a CharSequence
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 use String.contains
instead of String.startsWith
). The performFiltering
function is not run on the UI thread.
您再回到您作为一个 FilterResults
对象,其中包含一个对象
的值的(匹配的列表,可能是一个的ArrayList
)和 INT
的计数的是您的匹配列表的大小。
You then return your list of matches as a FilterResults
object, which contains an Object
values (your list of matches, probably an ArrayList
) and an int
count which is the size of your list of matches.
最后,落实<一个href=\"http://developer.android.com/reference/android/widget/Filter.html#publishResults%28java.lang.CharSequence,%20android.widget.Filter.FilterResults%29\"相对=nofollow> publishResults
回调方法,一旦工作线程已产生匹配列表,让您调用返回 notifyDataSetChanged
您AutoCompleteTextView的适配器上,以便它可以显示结果。
Finally, implement the publishResults
callback method, which returns once the worker thread has generated the list of matches, allowing you to call notifyDataSetChanged
on your AutoCompleteTextView's adapter so that it can display the results.
这篇关于自AutoCompleteTextView行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!