【问题标题】:Resetting an Autocompletetextview with a custom arrayadapter and filter使用自定义 arrayadapter 和过滤器重置 Autocompletetextview
【发布时间】:2014-04-21 21:29:02
【问题描述】:

我在使用自动完成文本视图时遇到了问题。当我第一次启动应用程序时,autocompletetextview 工作正常,我可以输入“m”并获取所有以“m”开头的餐厅的列表...

然后当我输入更多“标记”时,列表会按预期更新......

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

知道为什么清除文本不允许我“重新开始”过滤吗? 这是我的 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();
            }
        }
    }

【问题讨论】:

    标签: android filter android-arrayadapter autocompletetextview


    【解决方案1】:
    if (charSequence == null ||charSequence.length()==0) {
      results.values = locations;
      results.count = locations.size();
    
      Log.v("!!!", "cleared filter");
      locations = allLocations;
      notifyDataSetChanged();
    }
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-26
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      相关资源
      最近更新 更多