Android - Implementing search filter to a RecyclerView
Asked Answered
K

8

47

I need to implement a search feature based on an EditText to my RecyclerView. The list must be filtered while typing.

Here's the code :

DisplayAdapter.java this is the adapter

public class DisplayAdapter extends RecyclerView.Adapter<DisplayAdapter.MyViewHolder>{
    private List<DataHolder> displayedList;
    public class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView english_d;
        public MyViewHolder(View view){
              super(view);
              english_d = (TextView) view.findViewById(R.id.engword);
        }
   }
    public DisplayAdapter(List<DataHolder> displayedList){
        this.displayedList = displayedList;
      }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int position)    {
    // create a layout
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);

    return new  MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    DataHolder content = displayedList.get(position);
    holder.english_d.setText(content.getEnglish());
   }
@Override
public int getItemCount() {
    return displayedList.size();
   }
}

DataHolder.java

public class DataHolder {
private String english;


public DataHolder() {
}

public DataHolder(String english, String german) {
    this.english = english;
   
}

public String getEnglish() {
    return english;
}

public void setEnglish(String english) {
    this.english = english;
   }

}

and here's the Fragment that I am working with :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_two_fragment, container, false);

        rv = (RecyclerView) view.findViewById(R.id.list_view_english);
        rv.setHasFixedSize(true);
       
        inputSearch = (EditText) view.findViewById(R.id.inputSearch);
        
        rv.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        rv.setLayoutManager(layoutManager);

        
        DisplayContent();
        

        disp_adapter = new DisplayAdapter(displayedList);
        
        rv.setAdapter(disp_adapter);
   return view;
}
Kurland answered 23/11, 2016 at 0:36 Comment(1)
github.com/Wrdlbrnft/Searchable-RecyclerView-Demo. Refer this for searchable Recycler View and if you want a detailed tutorial refer this, studytutorial.in/android-search-to-recyclerview-tutorialScantling
G
124

in your adapter add new function for update the list

public void updateList(List<DataHolder> list){
     displayedList = list;
     notifyDataSetChanged();
}

add textWatcher for search lets say you are using Edittext as search field

searchField.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // filter your list from your input
            filter(s.toString());
            //you can use runnable postDelayed like 500 ms to delay search text
        }
    });

Simple filter function

void filter(String text){
     List<DataHolder> temp = new ArrayList();
     for(DataHolder d: displayedList){
           //or use .equal(text) with you want equal match
           //use .toLowerCase() for better matches
           if(d.getEnglish().contains(text)){
               temp.add(d);
           }
     }
     //update recyclerview
     disp_adapter.updateList(temp);
}


With Butterknife

@OnTextChanged(R.id.feature_manager_search)
protected void onTextChanged(CharSequence text) {
    filter(text.toString());
}
Gracia answered 23/11, 2016 at 6:0 Comment(6)
if(d.getEnglish().equal(text)){ temp.add(d); } change to if(d.getEnglish().contains(text)){ temp.add(d); }Swaddle
Thanks dude worked fine just d.getName().toLowerCase().contains(text.toLowerCase()) would get clear results thanksErland
it is working. [stackoverflow.com/users/3319074/zeroone](@ZeroOne)Laveen
This is great. But i just wanted to know is this method is efficient or with Filterable is efficient. However i dont see any back draw in this method also. ThanksGarrity
Easy to implement. Thank you sir!Leporide
Flawless. WORKS like a charm, thankyou so muchKolomna
J
4

i have done like this

.XMl file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="1dp"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:layout_marginBottom="@dimen/bottom_margin">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

<EditText
    android:id="@+id/etSearch"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Search"
    android:imeOptions="actionDone"
    android:singleLine="true"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/rvCategory"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>

.JAVA Class file

package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.ArrayList;

public class Home extends AppCompatActivity implements ResultCallBack{

SQLiteDatabase db;

ArrayList<String> alCategorName=new ArrayList<>();

RecyclerView rvCategory;
EditText etSearch;
Category bookadapter;
ArrayList<String> alBookInfo;

String strURL="http://localhost:21246/api/TreasuryBooks?BookId=1";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitleTextColor(getResources().getColor(R.color.white));
    setSupportActionBar(toolbar);

//        AsyncTask_WebAPI asyncTask=new AsyncTask_WebAPI(this,strURL,this);
//        asyncTask.execute();

    etSearch=(EditText)findViewById(R.id.etSearch);
    etSearch.setText("");

    DatabaseAccess da= DatabaseAccess.getInstance(this);
    db=da.open();
    alBookInfo=new ArrayList<String>();

    Cursor c = db.rawQuery("select BookName from Books", null);
    int rows = c.getCount();

    c.moveToFirst();

    for(int i=0;i<rows;i++)
    {
        String strBoookName=c.getString(c.getColumnIndex("BookName"));
        alBookInfo.add(strBoookName);
        c.moveToNext();
    }

    c.close();
    db.close();

    rvCategory=(RecyclerView)findViewById(R.id.rvCategory);

    //Recycler view adapter
    bookadapter=new Category(this,alBookInfo);
    rvCategory.setLayoutManager(new LinearLayoutManager(this));
    rvCategory.setAdapter(bookadapter);

    //text change listner
    etSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,             int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
           bookadapter.getFilter().filter(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

@Override
public void onBackPressed() {
    Home.this.finish();
}

@Override
protected void onRestart() {
    super.onRestart();
    etSearch.setText("");
}

@Override
public void onResultListener(Object object) {
    JSONObject jsonObject=(JSONObject)object;
    try {
        String strId = jsonObject.getString("id");
        String strBookId=jsonObject.getString("BookId");
        String strBookName=jsonObject.getString("BookName");

        db.rawQuery("insert into Books values('"+strId+"','"+strBookId+"','"+strBookName+"'",null);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

Recyclerview adapter

  //Recycler view adapter Class with filterable interface

   private class Category extends RecyclerView.Adapter<viewHolder> implements Filterable
{
    ArrayList<String> alBooks=new ArrayList<String>();
    Context context;
    viewHolder holder;

    public  Category()
    {}

    public Category(Context context,ArrayList<String> albooks)
    {
        this.context=context;
        this.alBooks=albooks;
    }

    @Override
    public int getItemCount() {
        return alBooks.size();
    }

    @Override
    public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v=    LayoutInflater.from(parent.getContext()).inflate(R.layout.home_cardview,parent,false);
        holder=new viewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(viewHolder holder, int position) {
        holder.tvBookName.setText(alBooks.get(position));
    }

    private Filter fRecords;

   //return the filter class object
    @Override
    public Filter getFilter() {
        if(fRecords == null) {
            fRecords=new RecordFilter();
        }
        return fRecords;
    }

   //filter class
    private class RecordFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();

            //Implement filter logic
    // if edittext is null return the actual list
            if (constraint == null || constraint.length() == 0) {
                //No need for filter
                results.values = alBookInfo;
                results.count = alBookInfo.size();

            } else {
               //Need Filter
        // it matches the text  entered in the edittext and set the data in adapter list
                ArrayList<String> fRecords = new ArrayList<String>();

                for (String s : alBookInfo) {
                    if (s.toUpperCase().trim().contains(constraint.toString().toUpperCase().trim())) {
                        fRecords.add(s);
                    }
                }
                results.values = fRecords;
                results.count = fRecords.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,FilterResults results) {

       //it set the data from filter to adapter list and refresh the recyclerview adapter
            alBooks = (ArrayList<String>) results.values;
            notifyDataSetChanged();
           }
       }
   }

   private class viewHolder extends  RecyclerView.ViewHolder {
    CardView cv;
    TextView tvBookName;

    viewHolder(final View itemview) {
        super(itemview);

        cv = (CardView) itemview.findViewById(R.id.cardview);
        tvBookName = (TextView) itemview.findViewById(R.id.categoryName);

        itemview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new       Intent(Home.this,Act_Fragment_Container.class);
                intent.putExtra("BookName", tvBookName.getText());
                startActivity(intent);
            }
        });
    }
   }

}

add recycler view adapter in same class to avoid confusion....

Jessi answered 20/3, 2017 at 11:43 Comment(3)
Note that android:singleLine="true" is deprecated, and it is recommended to use android:maxLines="1".Wan
Hi @Kishor it works super fine. Just when I clear the edittext after that it's not updating with the original data list.Fredel
@amit , You must be replacing the original list in publish results, Maintain a duplicate list which stores every item, and use it for performingFiltering method.Continence
A
4

We can apply search functionality in Recyclerview as below. This works 100% fine. I have implemented it on button click . It can be used in EditText's textchangeListner callback methods.

    filterIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String strCHR = homeSearchEdit.getText().toString();
            if (homeSearchEdit.getText().toString().length() > 0) {
                ArrayList<ServiceModel> listNew = new ArrayList<>();
                for (int l = 0; l < arrayList.size(); l++) {
                    String serviceName = arrayList.get(l).getServiceName().toLowerCase();
                    if (serviceName.contains(strCHR.toLowerCase())) {
                        listNew.add(arrayList.get(l));
                    }
                }
                recyclerView.setVisibility(View.VISIBLE);
                adapter = new ServiceAdapter(HomeActivity.this, listNew);
                recyclerView.setAdapter(adapter);
            } else {
                recyclerView.setVisibility(View.VISIBLE);
                adapter = new ServiceAdapter(HomeActivity.this, arrayList);
                recyclerView.setAdapter(adapter);
            }
        }
    });

where , fliterIcon is ImageButton . and ServiceAdapter is adapter.

Aperiodic answered 4/3, 2021 at 9:46 Comment(0)
E
2

Implement SearchView.OnQueryTextListener

public class Countries_List extends AppCompatActivity  implements SearchView.OnQueryTextListener {

    //your Model Class

    List<Countries> dataList, filteredDataList;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        MenuItem item=menu.findItem(R.id.action_search);
        SearchView searchView=(SearchView)MenuItemCompat.getActionView(item);
        searchView.setOnQueryTextListener(this);

        return true;
    }
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {

        filteredDataList = filter(dataList, newText);
        mAdapter.setFilter(filteredDataList);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_search) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    private List<Countries> filter(List<Countries> dataList, String newText) {
        newText=newText.toLowerCase();
        String text;
        filteredDataList=new ArrayList<>();
        for(Countries dataFromDataList:dataList){
            text=dataFromDataList.getTitle().toLowerCase();

            if(text.contains(newText)){
                filteredDataList.add(dataFromDataList);
            }
        }

        return filteredDataList;
    }
}

And, in your Adapter Class:

void setFilter(List<Countries> FilteredDataList) {
        countriesList = FilteredDataList;
        notifyDataSetChanged();
    }
Embryotomy answered 11/3, 2019 at 10:2 Comment(0)
O
2

In my case I created a base adapter to be reused.

abstract class RVAdapter<T>(
    internal val origValues: MutableList<T>,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>(), Filterable {

    // use this in base class to access items
    protected var values = origValues

    private var onNothingFound: (() -> Unit)? = null

    override fun getItemCount(): Int = values.size

    protected open fun searchCriteria(searchText: String, value: T) : Boolean {
        return true
    }

    fun search(s: String?, onNothingFound: (() -> Unit)? = null ) {
        this.onNothingFound = onNothingFound
        filter.filter(s)
    }

    override fun getFilter(): Filter {
        return object : Filter() {
            private val filterResults = FilterResults()
            override fun performFiltering(constraint: CharSequence?): FilterResults {
                val charSearch = constraint.toString()
                if (charSearch.isEmpty()) {
                    values = origValues
                } else {
                    values = ArrayList()
                    val searchResults = origValues.filter { searchCriteria(charSearch, it) }
                    values.addAll(searchResults)
                }
                return filterResults.also {
                    it.values = values
                }
            }

            override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
                // no need to use "results" filtered list provided by this method.
                values = results?.values as ArrayList<T>
                notifyDataSetChanged()
            }
        }
    }    
}

Use it like this.

class AssetTypeRecyclerViewAdapter(
    allValues: MutableList<AssetType>
) : RVAdapter<AssetType>(allValues) {

    override fun onBindViewHolder(bholder: RecyclerView.ViewHolder, position: Int) {
        val item = values[position]
        val holder = bholder as AssetTypeRecyclerViewAdapter.ViewHolder
        ...
    }

    override fun searchCriteria(searchText: String, value: AssetType) : Boolean {
        return value.name.toLowerCase(Locale.ROOT).startsWith(searchText)
    }
}

And inside the fragment, inflate the menu like you normally do, and in onCreateOptionsMenu ...

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    menu.clear()
    inflater.inflate(R.menu.fragment_list, menu)

    val searchView = menu.findItem(R.id.action_search).actionView as SearchView
    searchView.maxWidth = Int.MAX_VALUE

    searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
        override fun onQueryTextSubmit(query: String?): Boolean {
            assetTypeAdapter.search(query)
            return true
        }

        override fun onQueryTextChange(newText: String?): Boolean {
            assetTypeAdapter.search(newText)
            return true
        }

    }
    )
    super.onCreateOptionsMenu(menu, inflater)
}
Orvas answered 30/11, 2020 at 5:19 Comment(0)
Y
0

//simple and easy way 
  
ArrayList<CategoryModel> myList = new ArrayList<>();
        for(CategoryModel object: list){
            if (object.getName().toLowerCase().contains(str.toLowerCase())){

                myList.add(object);
            }
        }

        CategoryAdapter adapterM = new CategoryAdapter(myList);

        recyclerView.setAdapter(adapterM);
Yellowish answered 1/10, 2021 at 11:7 Comment(0)
M
0

Search Filter for RecyclerView In Android using Kotlin: -

       fun addTextListener() {
            ed_general.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable) {}
                override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
                override fun onTextChanged(query1: CharSequence, start: Int, before: Int, count: Int) {
                    var query = query1
                    query = query.toString().toUpperCase()
                    val filteredList: ArrayList<GetFavoritePojo.Data> = ArrayList()
                    for (i in 0 until arrayList.size) {
                        val text: String = arrayList.get(i).name.toUpperCase()
                        if (text.contains(query)) {
                            filteredList.add(arrayList.get(i))
                        }
                    }
                    editProfileFavoriteAdapter = EditProfileFavoriteAdapter(
                        requireActivity(),
                        filteredList)
                    recycler_fav.setAdapter(editProfileFavoriteAdapter)
                    editProfileFavoriteAdapter!!.notifyDataSetChanged() // data set changed
                }
            })
        }

In XML Use this Edittext : -

 <EditText
            android:id="@+id/ed_general"
            style="@style/edittext_style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_16sdp"
            android:background="@drawable/button_white_rounded_corners"
            android:drawableLeft="@drawable/ic_search"
            android:fontFamily="@font/balsamiq_sans_regular"
            android:hint="Search Here..."
            android:textSize="@dimen/_12sdp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view_1" />
Malamute answered 10/3, 2022 at 9:12 Comment(0)
F
0

1.Menu Code

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_menu,menu);
   MenuItem item = menu.findItem(R.id.action_search);
    SearchView searchView= (SearchView) item.getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            processText(query);
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            processText(newText);
            return true;
        }
    });
    return super.onCreateOptionsMenu(menu);
}
  1. Function code

    private void processText(String text){

      filterList.clear();
     for(Model model : list){
         String name = model.getCn();
         if (name.toLowerCase().contains(text.toLowerCase())){
             filterList.add(model);
         }
          notesAdapter.updateList(filterList);
    
     }
    

    }

Fanya answered 9/3, 2023 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.