重置一个Autocompletetextview一个自定义arrayadapter和过滤器 [英] Resetting an Autocompletetextview with a custom arrayadapter and filter

查看:590
本文介绍了重置一个Autocompletetextview一个自定义arrayadapter和过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与autocompletetextview麻烦。当我第一次启动应用程序的autocompletetextview工作的预期,我可以输入M,并得到它与M开头的所有餐厅...

列表

后来,当我种类较多,标记,如预期的列表更新...

然而,当我删除文字,并用不同的字母开始,在autocompletetextview不再与其他字母的更新问题发生...

任何想法,为什么清除文本不允许我从头再来的过滤功能?
这里是我的ArrayAdapter code ...

 类AutoLocationAdapter扩展ArrayAdapter<地点>实现可筛选{
    私人的ArrayList<地点>地点;
    私人的ArrayList<地点> allLocations;
    私人过滤器过滤;    公共AutoLocationAdapter(上下文的背景下,INT布局,ArrayList的<地点>的位置){
        超(背景下,布局,位置);
        this.locations =位置;
        this.allLocations =位置;
    }    @覆盖
    公众诠释的getCount(){
        返回locations.size();
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){        视图V = convertView;        如果(V == NULL){
            LayoutInflater吹气=(LayoutInflater)的getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
            V = inflater.inflate(R.layout.fragment_locations_auto_row,NULL);
        }        位置L = locations.get(位置);        如果(L!= NULL){            TextView的名字=(TextView中)v.findViewById(R.id.frag_location_auto_name);
            TextView的镇=(TextView中)v.findViewById(R.id.frag_location_auto_town);            如果(名字!= NULL){
                name.setText(l.getName());
            }
            如果(镇!= NULL){
                town.setText(l.getTown());
            }
        }        返回伏;
    }    @覆盖
    公共过滤用getFilter(){
        如果(过滤== NULL){
            过滤器=新LocationFilter();
        }
        返回过滤器;
    }    私有类LocationFilter扩展过滤器{        @覆盖
        保护FilterResults performFiltering(CharSequence中的CharSequence){
            FilterResults结果=新FilterResults();
            如果(CharSequence的== NULL || charSequence.length()== 0){
                results.values​​ =位置;
                results.count = locations.size();
                Log.v(!,清除过滤器);
                位置= allLocations;
                notifyDataSetChanged();
            }
            其他{
                ArrayList的<地点>存储单元数=新的ArrayList<地点>();
                对于(位置L:位置){
                    如果(l.getName()。与toUpperCase()。startsWith(charSequence.toString()。与toUpperCase())){
                        nLocations.add(升);
                    }
                }
                results.values​​ =存储单元数;
                results.count = nLocations.size();
            }
            返回结果;
        }        @覆盖
        保护无效publishResults(CharSequence中的CharSequence,FilterResults filterResults){
            如果(filterResults.count == 0){
                notifyDataSetInvalidated();
            }
            其他{
                位置=(ArrayList的<地点>)filterResults.values​​;
                notifyDataSetChanged();
            }
        }
    }


解决方案

 如果(CharSequence的== NULL || charSequence.length()== 0){
  results.values​​ =位置;
  results.count = locations.size();  Log.v(!,清除过滤器);
  位置= allLocations;
  notifyDataSetChanged();
}

所以,基本上,当你已经过滤,你说这里 results.values​​ =位置对应的 previous 过滤。因此,如果新的过滤模式不匹配,你previously过滤设定的,它不会显示任何东西,有什么你基本上经历。你必须首先设置位置= allLocations ,然后将结果* PARAMS:

 如果(CharSequence的== NULL || charSequence.length()== 0){
  Log.v(!,清除过滤器);
  位置= allLocations;
  notifyDataSetChanged();  results.values​​ =位置;
  results.count = locations.size();
}

I'm having trouble with an autocompletetextview. When I first start the app the autocompletetextview work's as expected, I can type in 'm' and get a list of all restaurants which start with 'm'...

Then when I type more, 'mark', the list updates as expected...

However the problem happens when I delete the text and start over with a different letter, the autocompletetextview no longer updates with other letters...

Any idea why clearing the text doesn't allow me to "start over" with the filtering? Here is my ArrayAdapter code...

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

解决方案

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

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

So, basically when you've already filtered, you're saying here that results.values = locations which corresponds to the previous filtering. So if the new filtering pattern don't match the set that you previously filtered, it won't show anything, what you're basically experiencing. You have to first set locations = allLocations and then set the results.* params:

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

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

这篇关于重置一个Autocompletetextview一个自定义arrayadapter和过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆