带有自定义 ArrayAdapter 和 Filter 的 AutoCompleteTextView

发布于 2024-12-22 18:10:13 字数 1343 浏览 1 评论 0原文

当我尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻身的咸鱼 2024-12-29 18:10:13

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

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();
        }
    }

The missing part is that I need to set the new values from filter so I just simply changed the

publushResults();

and now it's working. The correct code bellow.

    @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();
        }
    }
能怎样 2024-12-29 18:10:13

另一个更新 - 在输入和删除搜索文本框中的所有字符时,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();
        }
    }

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:

@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();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文