2

i have implemneted search functionality for custom adapter but stil is not working, i have posted my code if anything wrong correct me.

java code

@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;
        }
    };
StoopidDonut
  • 8,547
  • 2
  • 33
  • 51
venu
  • 2,971
  • 6
  • 40
  • 59

2 Answers2

0

This is my most usual implementation:

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

  return ownfilter;
}

Basically, ownfilter is an instance of CustomFilter class, which is an extension of the Filter class. In that Filter extension, you have to override the following methods:

  • protected FilterResults performFiltering(CharSequence): There you define how to perform the filtering over the CharSequence passed to the method. You have to return a FilterResults object, for which you'll have to set two fields: values, which is the List of filter matches, and count, which is basically the number of matcher (List.size()).

  • protected void publishResults(CharSequence, FilterResults): There is where you'll be passed the FilterResults from performFiltering, and all you have to do is to call notifyDataSetChanged() if count > 0 or notifyDataSetInvalidated() otherwise.

And that's it.

nKn
  • 13,691
  • 9
  • 45
  • 62
0

Use like this.. it worked perfectly for me

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();
    }

}
Siva
  • 446
  • 2
  • 9
  • 24