实现Filterable的Android适配器不显示结果

时间:2017-08-02 10:16:42

标签: android textview android-filterable

我有一个自定义适配器,用于过滤搜索结果。这适用于查找自动完成搜索标记。但是,它已被修改为在用户首次触摸搜索文本视图时显示最近搜索的项目。通过在搜索textView:

中将文本设置为“”来启动过滤过程
TextView.OnTouchListener onTouchListener = new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            searchTextView.setText("");
            recentSearches = true;

            return false;
        }
    };

还有一个Textwatcher:

TextWatcher onSearchSuggestionTextChanged = new TextWatcher()
    {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            if (s.length() > 0)
            {
             recentSearches = false;
            }
        }

        @Override
        public void afterTextChanged(Editable s)
        {
            if (!recentSearches && s.length() == 0)
            {
                cancelSearch();
            }
        }
    };

搜索textView初始化如下:

searchTextView.setAdapter(new SearchAdapter(this));
searchTextView.addTextChangedListener(onSearchSuggestionTextChanged);
searchTextView.setOnTouchListener(onTouchListener);

SearchAdapter是:

public class SearchAdapter extends BaseAdapter implements Filterable
{
    private Context context;
    private List<FoundItem> resultList = new ArrayList<FoundItem>();
    private List<FoundItem> recentSearches = null;

    public SearchAdapter(Context context)
    {
        this.context = context;
    }

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

    @Override
    public FoundItem getItem(int index)
    {
        return resultList.get(index);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.simple_dropdown_item, parent, false);
        }

        FoundItem item = getItem(position);
        ((TextView) convertView.findViewById(R.id.text)).setText(item.name());

        return convertView;
    }

    @Override
    public Filter getFilter()
    {
        Filter filter = new Filter()
        {
            @Override
            protected FilterResults performFiltering(CharSequence constraint)
            {
                FilterResults filterResults = new FilterResults();
                if (constraint != null)
                {
                    List<FoundItem> items = findSuggestions(context, constraint.toString());

                    // Assign the data to the FilterResults
                    filterResults.values = items;
                    filterResults.count = items.size();
                }
                else
                {
                    filterResults.values = getRecentSearches();
                    filterResults.count = getRecentSearches().size();
                }
                return filterResults;
            }

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

    private List<FoundItem> findSuggestions(Context context, String query)
    {
        ...
        return foundItems;
    }

    private List<FoundItem> getRecentSearches()
    {
        ...
        return recentSearches;
    }

适配器确实获得了最近搜索的结果,因此performFilteringpublishResults方法按预期调用,但只调用getCount方法一次,并且不显示任何结果。相反,在键入几个字符时,自动填充项目会按预期显示。请注意,最近的搜索不是null并且在调用时 在文本更改之前和之后searchTextView.getAdapter().getCount()返回正确的先前搜索次数。

那为什么不显示结果呢?有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

手动我们必须致电

onSearchSuggestionTextChanged.onTextChanged()

在edittext的ontouch(action_down)中,如果其中的文本长度为零!

将触发过滤器的所有事件

TextView.OnTouchListener onTouchListener = new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        searchTextView.setText("");
        recentSearches = true;
        onSearchSuggestionTextChanged.onTextChanged("",0,0,0);
        return false;
    }
};