2

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

爪哇代码

@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;
        }
    };
4

2 回答 2

0

像这样使用..它对我来说非常有用

private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String filterString = constraint.toString().toLowerCase();

        FilterResults results = new FilterResults();

        final ArrayList<HashMap<String, String>> list = all_details;

        int count = list.size();
        final ArrayList<HashMap<String, String>> newlist = new ArrayList<HashMap<String, String>>();

        String filterableString;

        for (int i = 0; i < count; i++) {
            filterableString = list.get(i).get("prodname");
            if (filterableString.toString().toLowerCase().contains(filterString)) {
                newlist.add(list.get(i));
            }
        }

        results.values = newlist;
        results.count = newlist.size();

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        if (results != null) {
            if (results.count > 0) {
                pt_details = new ArrayList<HashMap<String,String>>((ArrayList<HashMap<String, String>>) results.values) ;
            } else {
                pt_details.clear();
                //pt_details = all_details;
            }

        } else {
            pt_details = all_details;
        }
        notifyDataSetChanged();
    }

}
于 2014-11-03T10:30:34.813 回答
0

这是我最常用的实现:

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

  return ownfilter;
}

基本上,ownfilter是类的一个实例CustomFilter,它是类的扩展Filter。在该Filter扩展中,您必须覆盖以下方法:

  • protected FilterResults performFiltering(CharSequence):您可以在此处定义如何对CharSequence传递给方法的内容执行过滤。您必须返回一个FilterResults对象,您必须为其设置两个字段:values,这是List过滤器匹配的,以及count,基本上是匹配器的数量(List.size())。

  • protected void publishResults(CharSequence, FilterResults): 在哪里你会被传递FilterResultsfrom performFiltering,你所要做的就是打电话notifyDataSetChanged()ifcount > 0notifyDataSetInvalidated()else 。

就是这样。

于 2014-01-24T05:55:41.630 回答