使用自定义数组适配器和过滤器重置Autocompletetextview

时间:2014-03-16 19:04:47

标签: android filter android-arrayadapter autocompletetextview

我在使用autocompletetextview时遇到问题。当我第一次启动应用程序时,autocompletetextview按预期工作,我可以输入'm'并获取所有以'm'开头的餐馆列表...

Autocompletetextview with 'm'

然后当我输入更多'mark'时,列表会按预期更新...

Autocompletetextview with 'mark'

但是,当我删除文本并以不同的字母重新开始时会出现问题,autocompletetextview不再使用其他字母更新...

Autocompletetextview with 'n'

任何想法为什么清除文本都不允许我“重新开始”过滤? 这是我的ArrayAdapter代码......

 class AutoLocationAdapter extends ArrayAdapter<Location> implements Filterable{
    private ArrayList<Location> locations;
    private ArrayList<Location> allLocations;
    private Filter filter;

    public AutoLocationAdapter(Context context, int layout, ArrayList<Location> locations) {
        super(context, layout , locations);
        this.locations = locations;
        this.allLocations = locations;
    }

    @Override
    public int getCount() {
        return locations.size();
    }

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

        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.fragment_locations_auto_row, null);
        }

        Location l = locations.get(position);

        if (l != null) {

            TextView name = (TextView) v.findViewById(R.id.frag_location_auto_name);
            TextView town = (TextView) v.findViewById(R.id.frag_location_auto_town);

            if (name != null){
                name.setText(l.getName());
            }
            if (town != null){
                town.setText(l.getTown());
            }
        }

        return v;
    }

    @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new LocationFilter();
        }
        return filter;
    }

    private class LocationFilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults results = new FilterResults();
            if (charSequence == null ||charSequence.length()==0) {
                results.values = locations;
                results.count = locations.size();
                Log.v("!!!", "cleared filter");
                locations = allLocations;
                notifyDataSetChanged();
            }
            else {
                ArrayList<Location> nLocations = new ArrayList<Location>();
                for(Location l : locations){
                    if(l.getName().toUpperCase().startsWith(charSequence.toString().toUpperCase())){
                        nLocations.add(l);
                    }
                }
                results.values = nLocations;
                results.count = nLocations.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            if (filterResults.count==0) {
                notifyDataSetInvalidated();
            }
            else{
                locations = (ArrayList<Location>) filterResults.values;
                notifyDataSetChanged();
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

if (charSequence == null ||charSequence.length()==0) {
  results.values = locations;
  results.count = locations.size();

  Log.v("!!!", "cleared filter");
  locations = allLocations;
  notifyDataSetChanged();
}

所以,基本上当你已经过滤时,你在这里说results.values = locations对应于之前的过滤。因此,如果新过滤模式与您之前过滤的集合不匹配,则它不会显示任何内容,您基本上会遇到的内容。您必须先设置locations = allLocations,然后设置results.*参数:

if (charSequence == null ||charSequence.length()==0) {
  Log.v("!!!", "cleared filter");
  locations = allLocations;
  notifyDataSetChanged();

  results.values = locations;
  results.count = locations.size();
}