Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有自定义阵列适配器和过滤器的AutoCompleteTextView_Android_Filter_Android Arrayadapter_Autocompletetextview - Fatal编程技术网

Android 带有自定义阵列适配器和过滤器的AutoCompleteTextView

Android 带有自定义阵列适配器和过滤器的AutoCompleteTextView,android,filter,android-arrayadapter,autocompletetextview,Android,Filter,Android Arrayadapter,Autocompletetextview,当我试图从LogCat中过滤AutoCompleteTextView中的结果时,我遇到了一个问题。我知道过滤的执行是正确的,但它没有刷新视图:/I我忘记了什么吗?有什么建议或帮助吗 下面是过滤器的源代码 @Override public Filter getFilter() { Filter myFilter = new Filter() { @Override protected FilterResults performFiltering(CharS

当我试图从LogCat中过滤AutoCompleteTextView中的结果时,我遇到了一个问题。我知道过滤的执行是正确的,但它没有刷新视图:/I我忘记了什么吗?有什么建议或帮助吗

下面是过滤器的源代码

@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;
}
@覆盖
公共过滤器getFilter(){
过滤器myFilter=新过滤器(){
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
i(标记“使用约束执行过滤:”+constraint.toString());
List resultsSuggestions=new ArrayList();
Log.i(标记“COUNT:+getCount());
对于(int i=0;i0){
notifyDataSetChanged();
}否则{
notifyDataSetionValidated();
}
}
};
返回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();
        }
    }
@覆盖
@抑制警告(“未选中”)
受保护的void publishResults(CharSequence约束、FilterResults结果){
清除();
ArrayList newValues=(ArrayList)results.values;
对于(int i=0;i0){
notifyDataSetChanged();
}否则{
notifyDataSetionValidated();
}
}

另一个更新-在输入和删除搜索文本框中的所有字符时,newValues.size()或newValues.get(i)上的应用程序会很快崩溃,因为newValues可能为空。下面是您应该使用的代码:

@Override
    @SuppressWarnings("unchecked")
    protected void publishResults(CharSequence constraint, FilterResults results) {
        clear();
        ArrayList<Suggestions> newValues = (ArrayList<Suggestions>) results.values;
        if(newValues !=null) {
            for (int i = 0; i < newValues.size(); i++) {
                add(newValues.get(i));
            }
            if(results.count>0){
                notifyDataSetChanged();
            } else{
                notifyDataSetInvalidated();
        }
    }
@覆盖
@抑制警告(“未选中”)
受保护的void publishResults(CharSequence约束、FilterResults结果){
清除();
ArrayList newValues=(ArrayList)results.values;
if(newValues!=null){
对于(int i=0;i0){
notifyDataSetChanged();
}否则{
notifyDataSetionValidated();
}
}

@Robert,你的不是复制品,是他的。