简体   繁体   English

AutoCompleteTextView筛选器不显示筛选列表

[英]AutoCompleteTextView Filter doesn't show the filtered list

I have a problem with my filter in my custom Adapter for my AutoCompleteTextView. 我的AutoCompleteTextView的自定义适配器中的过滤器出现问题。 It doesn't want to show the filtered list. 它不想显示过滤的列表。 I know that the filter filters my list because for example I have "Paris/FRANCE" in my list and when I type it in the AutoCompleteTextView, the list is still shown but it continues to show the complete list and not the filtered list. 我知道过滤器会过滤我的列表,因为例如我的列表中有“巴黎/法国”,并且当我在AutoCompleteTextView中键入它时,该列表仍会显示,但会继续显示完整列表,而不是已过滤列表。 If I type "Pary", the list disappear after typing the "Y" letter. 如果我键入“ Pary”,则在键入“ Y”字母后该列表将消失。 That's why I know that the filter works. 这就是为什么我知道过滤器可以工作的原因。

Here is my code: 这是我的代码:

public class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable
{
    private ArrayList<String> data;

    public AutoCompleteAdapter(Context context, int resource, ArrayList<String> objects) 
    {
        super(context, resource, objects);
        data = objects;
    }



@Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View v = convertView;

        if (v == null) 
        {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.item, null);
        }

        String s = getItem(position);

        if (s != null) 
        {
            TextView city = (TextView)v.findViewById(R.id.city);
            TextView country = (TextView)v.findViewById(R.id.country);

            String[] split = s.split("/");
            city.setText(split[0]);
            country.setText(split[1]);
        }

        return v;

    }

    @Override
    public Filter getFilter()
    {
        return new Filter()
        {

            @Override
            protected FilterResults performFiltering(CharSequence prefix)
            {
                FilterResults fr = new FilterResults();
                if(prefix != null)
                {
                    ArrayList<String> filtered = new ArrayList<String>();
                    for(String s : data)
                    {
                        if(s.toLowerCase().contains(prefix.toString().toLowerCase()))
                        {
                            filtered.add(s); 
                        }
                    }

                    fr.values = filtered;
                    fr.count = filtered.size();
                }
                return fr;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence contraint, FilterResults results) 
            {
                data = (ArrayList<String>)(results.values);

                if(results != null && results.count > 0) 
                {
                    notifyDataSetChanged();
                }
                else 
                {
                    notifyDataSetInvalidated();
                }
            }

        };
    }
    }

Please forgive me for my bad english and I hope you will help me. 请原谅我英语不好,希望您能帮我。 Thanks 谢谢

You never filter the base class, so methods that you don't overwrite behave the same way they would, if no filtering was done. 您永远不会过滤基类,因此,如果不进行过滤,则不会覆盖的方法将以相同的方式运行。

Eg getCount() still uses the old list, since you replace the reference to the list in the data field, but not the reference in the ArrayAdapter base class. 例如,由于您替换了data字段中对列表的引用,而不是ArrayAdapter基类中的引用,因此getCount()仍使用旧列表。

If you modify data instead of replacing it, it should work: 如果您修改data而不是替换data ,则它应该起作用:

Replace 更换

data = (ArrayList<String>)(results.values);

with

data.clear();
data.addAll((ArrayList<String>)results.values);

However you may want to keep a backup of the original list somewhere, in case someone deletes letters from the string you use to filter (and therefore filtering of the filtered list would no longer produce the same result as filtering the original list with the current filter directly). 但是,您可能希望将原始列表的备份保存在某个地方,以防万一有人从用于过滤的字符串中删除字母(因此,过滤列表的过滤将不再产生与使用当前过滤器过滤原始列表相同的结果。直)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM