Skip to content Skip to sidebar Skip to footer

How To Set Filter For Recycler View If Search Query Resulting No Match In Search View

This code works fine if the item we entered in search view matches but if we enter some query which does not match ...application is crashing in android. Here Main_ATMItemList is t

Solution 1:

You need to abstract RecyclerView.Adapter and make it inherit Filterable. Then you subclass this abstraction to the adapter you want to create.

Override getFilter() method to instantiate Filter. Refer to examples how to filter an adapter and move the logic you have now, in performFiltering(String constraint) method of the Filter

You should do the filtering in background thread, hence the Filterable. Filterable does the filtering on background thread and delivers the result on UI thread or thread that called filtering.

Like this you can achieve somewhat of compatibility to old good ListView.

Someone in the comments asked for example:

publicabstractclassBaseFilterableRecyclerViewAdapter<VH extendsRecyclerView.ViewHolder> extendsRecyclerView.Adapter<VH> implementsFilterable {

    private Context mContext;
    publicBaseFilterableRecyclerViewAdapter(Context context) {
        this.mContext = context;
    }
    publicabstractvoidsort(SortingFilter.Sort sortingStrategy);

}

In the extended class:

publicclassProductAdapterextendsBaseFilterableRecyclerViewAdapter<RecyclerView.ViewHolder>{

//------- Other methods ----@Overridepublic Filter getFilter() {
        returnnewSortingFilter(mData) {
            @OverrideprotectedvoidpublishResults(CharSequence constraint, FilterResults results) {
                if (results.values != null) {
                    intlast= mData.size();
                    mData = (List<? extendsProduct>) results.values;
                    notifyItemRangeChanged(mHeaderView == null ? 0 : 1, last);
                }
            }
        };
    }


}

Solution 2:

Use this works for me all time

privateList<ExampleModel> filter(List<ExampleModel> models, String query) {
    query = query.toLowerCase();

    finalList<ExampleModel> filteredModelList = new ArrayList<>();
    for (ExampleModel model : models) {
        finalString text = model.getText().toLowerCase();
        if (text.contains(query)) {
            filteredModelList.add(model);
        }
    }
    return filteredModelList;
}

Then onQueryTextChange(String query) call the filter

final List<ExampleModel> filteredModelList =filter(mModels, query);
        mAdapter.animateTo(filteredModelList);
        mRecyclerView.scrollToPosition(0);

below is the animateTo method found in the Adapter

publicvoidanimateTo(List<ExampleModel> models) {
        applyAndAnimateRemovals(models);
        applyAndAnimateAdditions(models);
        applyAndAnimateMovedItems(models);
    }

privatevoidapplyAndAnimateRemovals(List<ExampleModel> newModels) {
    for (inti= mModels.size() - 1; i >= 0; i--) {
        finalExampleModelmodel= mModels.get(i);
        if (!newModels.contains(model)) {
            removeItem(i);
        }
    }
}

privatevoidapplyAndAnimateAdditions(List<ExampleModel> newModels) {
    for (inti=0, count = newModels.size(); i < count; i++) {
        finalExampleModelmodel= newModels.get(i);
        if (!mModels.contains(model)) {
            addItem(i, model);
        }
    }
}

privatevoidapplyAndAnimateMovedItems(List<ExampleModel> newModels) {
    for (inttoPosition= newModels.size() - 1; toPosition >= 0; toPosition--) {
        finalExampleModelmodel= newModels.get(toPosition);
        finalintfromPosition= mModels.indexOf(model);
        if (fromPosition >= 0 && fromPosition != toPosition) {
            moveItem(fromPosition, toPosition);
        }
    }
}

public ExampleModel removeItem(int position) {
    finalExampleModelmodel= mModels.remove(position);
    notifyItemRemoved(position);
    return model;
}

publicvoidaddItem(int position, ExampleModel model) {
    mModels.add(position, model);
    notifyItemInserted(position);
}

publicvoidmoveItem(int fromPosition, int toPosition) {
    finalExampleModelmodel= mModels.remove(fromPosition);
    mModels.add(toPosition, model);
    notifyItemMoved(fromPosition, toPosition);
}

}

Post a Comment for "How To Set Filter For Recycler View If Search Query Resulting No Match In Search View"