我已经为自定义适配器实现了搜索功能,但仍然无法正常工作,如果有任何错误纠正了我,我已经发布了我的代码。
Java代码

@Override
    public Filter getFilter() {
        /**
         * A filter object which will filter message key
         * */

        return filter;
    }

    Filter filter = new Filter() {

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {

            mEventUtil = (List<Event>) results.values;

            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();



            if (mOriginalValues == null) {
                mOriginalValues = new ArrayList<Event>(mEventUtil); // mOriginalValues

            }

//          if (mListItem == null) {
//              mListItem = new ArrayList<String>();
//              for (Event message : mOriginalValues) {
//                  mListItem.add(message.getEvent_Title());
//              }
//          }

            /**
             *
             * If constraint(CharSequence that is received) is null returns
             * the mOriginalValues(Original) values else does the Filtering
             * and returns FilteredArrList(Filtered)
             *
             **/

            if (constraint == null || constraint.length() == 0) {

                /*
                 * CONTRACT FOR IMPLEMENTING FILTER : set the Original
                 * values to result which will be returned for publishing
                 */
                results.count = mOriginalValues.size();
                results.values = mOriginalValues;
            } else {
                /* Do the filtering */
                constraint = constraint.toString().toLowerCase();
                List<Event> FilteredArrList = new ArrayList<Event>(mOriginalValues.size());
                for (int i = 0; i < mOriginalValues.size(); i++) {
                    Event event = mOriginalValues.get(i);

                    if (event.getEvent_Title().toLowerCase().contains(constraint.toString())) {
                        FilteredArrList.add(event);
                    }else{

                    }
                }

                // set the Filtered result to return
                results.count = FilteredArrList.size();
                results.values = FilteredArrList;
            }
            return results;
        }
    };

最佳答案

这是我最常用的实现:

@Override
public Filter getFilter() {
  if (ownfilter == null)
    ownfilter = new CustomFilter();

  return ownfilter;
}


基本上,ownfilterCustomFilter类的实例,是Filter类的扩展。在该Filter扩展名中,您必须重写以下方法:


protected FilterResults performFiltering(CharSequence):您可以在其中定义如何对传递给方法的CharSequence进行过滤。您必须返回一个FilterResults对象,必须为其设置两个字段:values(它是过滤器匹配项的List)和count(这基本上是匹配器的数目)(列表。尺寸())。
protected void publishResults(CharSequence, FilterResults):在哪里可以通过FilterResults传递performFiltering,如果notifyDataSetChanged()count > 0,则只需调用notifyDataSetInvalidated()


就是这样。

10-08 18:11