为什么Filter.publishResults()不在适配器内部触发?

时间:2019-03-09 13:49:51

标签: android android-adapter searchview android-filterable android-filter

我目前将在我的应用程序中为SearchView实现一个自定义数组适配器。 我将从远程服务器返回自动完成建议,所以我尝试实现自定义过滤器。

问题出在我的自定义过滤器performFiltering内,但从未调用publishResults,并且未应用任何过滤器就返回了所有阵列适配器项的列表。

我的代码在下面,如果工作正常,则过滤后必须显示3个固定项目,分别是 ttt xyz test ,但我解释了这不会发生,并且item是数组元素,没有任何更改和过滤。

SearchArrayAdapter

class SearchArrayAdapter extends ArrayAdapter<String> implements Filterable {


    public SearchArrayAdapter(@NonNull Context context, int resource) {
        super(context, resource);
    }

    public SearchArrayAdapter(@NonNull Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

    public SearchArrayAdapter(@NonNull Context context, int resource, @NonNull String[] objects) {
        super(context, resource, objects);
    }

    public SearchArrayAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull String[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    public SearchArrayAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
        super(context, resource, objects);
    }

    public SearchArrayAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<String> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        Log.v("MY_TAG", "Adapter Item:" + getItem(position));
        view.setTag(getItem(position));
        return view;
    }

    @NonNull
    @Override
    public Filter getFilter() {
        Filter myFilter = new Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                ArrayList<String> x = new ArrayList<>();
                String[] arrayofString = (new String[]{"ttt", "xyz", "test"});
                Collections.addAll(x, arrayofString);
                filterResults.values = x;
                filterResults.count = 3;
                return filterResults;
            }

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

在我的片段里面:

SearchArrayAdapter adapter = new SearchArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, searchItems);
mSearchAutoComplete.setAdapter(adapter);
mSearchAutoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.v("MY_TAG", "Adapter Position: " + position);
        Log.v("MY_TAG", "Adapter String: " + searchItems[position]);
        Log.v("MY_TAG", "Adapter TAG: " + view.getTag());

        searchView.setQuery((String) view.getTag(), true);


    }
});
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
    @Override
    public boolean onSuggestionSelect(int position) {
        Log.v("MY_TAG", "Item Selected: " + position);
        return true;
    }

0 个答案:

没有答案