gpt4 book ai didi

android - 带有自定义 ArrayAdapter 和 Filter 的 AutoCompleteTextView

转载 作者:行者123 更新时间:2023-11-29 18:15:06 25 4
gpt4 key购买 nike

当我尝试从 LogCat 过滤 AutoCompleteTextView 中的结果时遇到问题我知道过滤执行正确但它没有刷新 View :/我是否忘记了任何建议或帮助?

这是过滤器的源代码。

@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;
}

最佳答案

缺少的部分是我需要设置过滤器的新值所以我只是简单地改变了

publushResults();

现在它正在工作。正确的代码如下。

    @Override
@SuppressWarnings("unchecked")
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
ArrayList<Suggestions> newValues = (ArrayList<Suggestions>) results.values;
for (int i = 0; i < newValues.size(); i++) {
add(newValues.get(i));
}
if(results.count>0){
notifyDataSetChanged();
} else{
notifyDataSetInvalidated();
}
}

关于android - 带有自定义 ArrayAdapter 和 Filter 的 AutoCompleteTextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8614393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com