Ololoking Ololoking - 2 years ago 253
Android Question

Search throug RecyclerView using Searchview

I want to search through

, I have
(BaseOfCards is my getter&setter class)
My RecyclerViewAdapter :

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

private LayoutInflater inflater;
private List<BaseOfCards> items;

//private int itemLayout;
//String cardvalue;
private Activity mActivity;

public RecyclerViewAdapter(Activity mActivity, Context context, List<BaseOfCards> items) {
this.mActivity = mActivity;
inflater = LayoutInflater.from(context);
this.items = items;
//this.itemLayout = itemLayout;

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = inflater.inflate(R.layout.custom_row, parent, false);
MyViewHolder holder = new MyViewHolder(view, mActivity);
return holder;

public void onBindViewHolder(MyViewHolder holder, int position) {

BaseOfCards item = items.get(position);

public int getItemCount() {

return items.size();

public static class MyViewHolder extends RecyclerView.ViewHolder {
private Activity mActivity;
TextView title;
ImageView titileImageView;

public MyViewHolder(View itemView, Activity mActivity) {


titileImageView = (ImageView) itemView.findViewById(R.id.image_country);

title = (TextView) itemView.findViewById(R.id.listText);
this.mActivity = mActivity;



I add SearchView to my menu and initialize it in MainActivity:

MenuItem menuItem = menu.findItem(R.id.action_search1);
searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

What do I need to do next? Make my RecyclerViewAdapter
implement Filterable
or what? Or just make class
in *RecyclerViewAdapter** and just call it from my MainActivity ?

Answer Source

I solved my problem

  1. Make my class RecyclerViewAdapter implements Filterable

  2. Add line private List<BaseOfCards> orig;

  3. Add method getFilter in RecyclerViewAdapter

    public Filter getFilter() {
    return new Filter() {
        protected FilterResults performFiltering(CharSequence constraint) {
            final FilterResults oReturn = new FilterResults();
            final List<BaseOfCards> results = new ArrayList<BaseOfCards>();
            if (orig == null)
                orig  = items;
                if (constraint != null){
                    if(orig !=null & orig.size()>0 ){
                        for ( final BaseOfCards g :orig) {
                            if (g.getCardName().toLowerCase().contains(constraint.toString()))results.add(g);
                    oReturn.values = results;
                return oReturn;
        protected void publishResults(CharSequence constraint, FilterResults results) {
            items = (ArrayList<BaseOfCards>)results.values;
  4. Make MainActivity implements SearchView.OnQueryTextListener and change method onQueryTextChange :

    public boolean onQueryTextChange(String newText) {
        if ( TextUtils.isEmpty ( newText ) ) {
        } else {
        return true;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download