sergejsha / pinned-section-listview

Easy to use ListView with pinned sections for Android.
2.59k stars 808 forks source link

Filter doesn't work #57

Closed Bubumuk closed 9 years ago

Bubumuk commented 9 years ago

I tried to implement Filterable and add a custom Filter, but the list doesn't seem to respond to the filter.

sergejsha commented 9 years ago

Code example?

Bubumuk commented 9 years ago

AdapterClass:

public class customAdapter extends ArrayAdapter<POJO> implements PinnedSectionListAdapter, Filterable {
@Override
public Filter getFilter() {
    if (filterObj == null) {
        filterObj = new FilterObj();
    }  
    return filterObj;
}
private class FilterObjextends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            // We implement here the filter logic
            ....
            }
            else {
                // We perform filtering operation
                ...
                }
                .....
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            // Now we have to inform the adapter about the new list filtered
            if (results.count == 0)
                notifyDataSetInvalidated();
            else {
                ...
                notifyDataSetChanged();
            }
        }

    }

I use a FastScrollAdapter also.

And in the onCreateOptionsMenu of the activity:

//Create the search view
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setQueryHint("Buscar..");
searchView.setOnQueryTextListener(new OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        mListObjAdapter.getFilter().filter(newText);
        return false;
    }
});

Cheers.

sergejsha commented 9 years ago

List responds on notifyDataSetChanged() call. If your dataset is correct, it must be shown. It looks that you do it properly. Just make sure adapter's data after filtering is not empty. Please see example app for more details on how it works.