退格时,Android自定义autocompletetextview会冻结

时间:2017-04-10 10:37:22

标签: android autocompletetextview

我在android中使用自定义适配器的autocompletetextview。它工作正常。但是,当我单击退格键从自动完成文本视图中清除所选项目时,它会冻结,或者删除时会有延迟。我怎么能克服这个?我的过滤器代码如下所示

 @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if (constraint != null) {
            suggestions.clear();
            FilterResults filterResults = new FilterResults();
            for (Names people : tempItems) {


  if(people.getName().toLowerCase().contains(constraint.toString()
  .toLowerCase())) {
                    suggestions.add(people);
                }
            }
       //     FilterResults filterResults = new FilterResults();
            filterResults.values = suggestions;
            filterResults.count = suggestions.size();
            return filterResults;
        } else {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults 
  results) {
        List<Names> filterList = (ArrayList<Names>) results.values;
        if (results != null && results.count > 0) {
            clear();
            for (Names people : filterList) {
                add(people);
                notifyDataSetChanged();
            }
        }
    }
};

我做错了什么?在此先感谢。

1 个答案:

答案 0 :(得分:0)

根据以下代码修改过滤器。您正在通知循环内的适配器,一旦整个列表被添加到另一个列表中,即在循环之外,应该通知它。

//这是我的整个适配器,就像一个魅力和关于UI线程的更新,是的,我正在通知UI线程上的适配器。

   public class SuburbSuggestionsAdapter extends ArrayAdapter<String> implements Filterable {

    protected static final String TAG = "SuggestionAdapter";
    private List<String> suggestions;
    private Context context;
    private List<String> SuburbList = new ArrayList<>();

    public SuburbSuggestionsAdapter(Context context) {
        super(context, android.R.layout.simple_dropdown_item_1line);
        suggestions = new ArrayList<String>();
        this.context = context;
    }

    @Override
    public int getCount() {
        return (null != suggestions ? suggestions.size() : 0);
    }

    @Override
    public String getItem(int index) {
        return suggestions.get(index);
    }

    @Override
    public Filter getFilter() {
        Filter myFilter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();

                if(!TextUtils.isEmpty(constraint)){
                    WebServiceHandler.hitFetchSuburbList(context, constraint.toString(), new CallbackRest() {
                        @Override
                        public void onDone(String response) {
                            if(SuburbList.size()!=0){
                                SuburbList.clear();
                            }
                            try {
                                JSONArray jsonArray = new JSONArray(response);
                                for(int i=0; i<jsonArray.length(); i++){
                                    SuburbList.add(jsonArray.get(i).toString());
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
                    // A class that queries a web API, parses the data and
                    // returns an ArrayList<GoEuroGetSet>
                    List<String> new_suggestions = SuburbList;
                    suggestions.clear();
                    for (int i=0;i<new_suggestions.size();i++) {
                        suggestions.add(new_suggestions.get(i));
                    }

                    // Now assign the values and count to the FilterResults
                    filterResults.values = suggestions;
                    filterResults.count = suggestions.size();
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence contraint,   FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };
        return myFilter;
    }

}