自定义getFilter()

时间:2014-04-19 05:48:53

标签: android filtering android-arrayadapter

我已经在互联网上寻找答案,似乎无法找到为什么我的自定义getFilter()不起作用。这是代码。

@Override
public Filter getFilter() 
{
    return new Filter()
    {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) 
        {
            FilterResults results = new FilterResults();

            if(charSequence == null || charSequence.length() == 0)
            {
                results.values = original_list;
                results.count = original_list.length;
            }else
            {
                List<String> filteredResults = new ArrayList<String>();

                for(int i = 0; i < original_list.length; i++)
                {
                    if(original_list[i].toString().toLowerCase().contains(charSequence))
                    {
                        filteredResults.add(original_list[i]);
                    }
                }

                String[] list = new String[filteredResults.size()];

                for(int i = 0; i < list.length; i++)
                {
                    list[i] = filteredResults.get(i);
                }

                results.count = list.length;
                results.values = list;
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults filteredResults)
        {
            filtered_list = (String[])filteredResults.values;
            notifyDataSetChanged();
        }
        };
    }
}

我最终试图弄清楚为什么当我使用字符串数组时,ArrayAdapter过滤工作完美,但是我的自定义过滤器每次输入我的EditText时都会崩溃

0 个答案:

没有答案