【问题标题】:AutoCompleteTextView Filter doesn't show the filtered listAutoCompleteTextView 过滤器不显示过滤后的列表
【发布时间】:2014-06-23 08:26:31
【问题描述】:

我的 AutoCompleteTextView 的自定义适配器中的过滤器有问题。它不想显示过滤后的列表。我知道过滤器过滤了我的列表,因为例如我的列表中有“Paris/FRANCE”,当我在 AutoCompleteTextView 中键入它时,列表仍然显示,但它继续显示完整列表而不是过滤列表。如果我键入“Pary”,则在键入“Y”字母后列表会消失。这就是我知道过滤器有效的原因。

这是我的代码:

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

        };
    }
    }

请原谅我英语不好,希望你能帮助我。谢谢

【问题讨论】:

  • 我建议你在你的getFilter()方法中设置一个断点,并使用调试器来找出发生了什么。

标签: java android autocompletetextview


【解决方案1】:

您永远不会过滤基类,因此如果没有进行过滤,您不会覆盖的方法的行为方式与它们相同。

例如getCount() 仍然使用旧列表,因为您替换了 data 字段中对列表的引用,而不是 ArrayAdapter 基类中的引用。

如果你修改data而不是替换它,它应该可以工作:

替换

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

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

但是,您可能希望在某处保留原始列表的备份,以防有人从您用于过滤的字符串中删除了字母(因此过滤已过滤列表将不再产生与过滤原始列表相同的结果)直接使用当前过滤器)。

【讨论】:

  • 感谢您的解释! Danke schön :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 2020-06-28
  • 2019-05-24
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多