带有自定义 ArrayAdapter 和 Filter 的 AutoCompleteTextView
当我尝试从 LogCat 过滤 AutoCompleteTextView 中的结果时遇到问题我知道过滤执行正确,但它没有刷新视图:/我是否忘记了任何建议或帮助?
这是过滤器的源代码。
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.i(TAG, "Perform filtering with constraint: " + constraint.toString());
List<String> resultsSuggestions = new ArrayList<String>();
Log.i(TAG, "COUNT: " + getCount());
for (int i = 0; i < getCount(); i++) {
if(getItem(i).getSuggestionValue().startsWith(constraint.toString())){
Log.i(TAG, "ADDED");
resultsSuggestions.add(getItem(i).getSuggestionValue());
}
}
FilterResults results = new FilterResults();
results.values = resultsSuggestions;
results.count = resultsSuggestions.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return myFilter;
}
I have a problem when I'm trying to Filter results in AutoCompleteTextView from LogCat I know that filtering is performed correct but it's not refreshing the view :/ Did I forget about something any suggestions or help?
Here is source code of filter.
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.i(TAG, "Perform filtering with constraint: " + constraint.toString());
List<String> resultsSuggestions = new ArrayList<String>();
Log.i(TAG, "COUNT: " + getCount());
for (int i = 0; i < getCount(); i++) {
if(getItem(i).getSuggestionValue().startsWith(constraint.toString())){
Log.i(TAG, "ADDED");
resultsSuggestions.add(getItem(i).getSuggestionValue());
}
}
FilterResults results = new FilterResults();
results.values = resultsSuggestions;
results.count = resultsSuggestions.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return myFilter;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
缺少的部分是我需要从过滤器设置新值,所以我只是简单地更改了
,现在它正在工作。正确的代码如下。
The missing part is that I need to set the new values from filter so I just simply changed the
and now it's working. The correct code bellow.
另一个更新 - 在输入和删除搜索文本框中的所有字符时,newValues.size() 或 newValues.get(i) 上的应用程序很快就会崩溃,因为 newValues 可能为空。所以,这是您应该使用的代码:
Another Update - On inputting and removing all the characters in search textbox very quickly crashes the application on newValues.size() or newValues.get(i) as newValues might be null. So, here's the code you should use: