Learn Searching within RecyclerView in 2 mins

Learn Searching within RecyclerView in 2 mins

Many Applications implement searching options over a list of items. So in this article, I will simplify this process. Let's get started...

So before implementing searching on a list of items, let's assume:

  1. We have a recycler view with let's say title and description.
  2. We have the required adapter defined for it.
  3. We have a search bar (or any type of input layout).

*Note - Assuming that we will search on the basis of title

If you have all the above points already implemented, we can start with the implementation. So lets make it super simple:

Step 1 :

Make a function taking a string(charSequence which is the input of the user) that contains a new list (suppose a filteredList).

//java
private void search(String userInput){
     List<ListModel> filteredList = new ArrayList<>();
}

//kotlin
 private fun search(s: String) {
        val filteredList: MutableList<ListModel> = ArrayList()

    }

Step 2:

Run a loop over the mail list and check whether the charsequence is present on the list (title) or not.

//java
private void search(String userInput){
     List<ListModel> filteredList = new ArrayList<>();
     for (ListModel item : mainList) {
            if (item.getTitle().contains(userInput)) {
                filteredList.add(item);
            }
        }
}

//kotlin
 private fun search(userInput: String) {
        val filteredList: MutableList<ListModel> = ArrayList()
        for (item in mainList) {
            if (item.title.contains(s)) {
                filteredList.add(item)
            }
        }
    }

Step 3:

Now make a function inside the adapter which takes the filteredList as argument let's say filterAdapter(List filteredList). This function simply stores the current list to filteredList and calls notifyDataSetChanged().

//java
public void filterAdapter(List<ListModel> filteredList){
   list = filteredList;
   notifyDataSetChanged();
}

//kotlin
private fun filterAdapter(filteredList: MutableList<ListModel>){
   list = filteredList
   notifyDataSetChanged()
}

Step 4:

Now call this above function inside our search() as

//java
private void search(String userInput){
     List<ListModel> filteredList = new ArrayList<>();
     for (ListModel item : mainList) {
            if (item.getTitle().contains(userInput)) {
                filteredList.add(item);
            }
        }
   adapter.filterAdapter(filteredList);
}

//kotlin
private fun search(userInput: String) {
        val filteredList: MutableList<ListModel> = ArrayList()
        for (item in mainList) {
            if (item.title.contains(s)) {
                filteredList.add(item)
            }
        }
      adapter.filterAdapter(filteredList)
    }

Step 5:

Now add event listener (TextWatcher) on Search Layout. Call the search function on afterTextChanged() callback and pass the Editable s after converting into string.

//java
 binding.searchLayout.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) {


            }

            @Override
            public void afterTextChanged(Editable s) {
                search(s.toString());
            }
        });

//kotlin
    binding.searchLayout.addTextChangedListener(object : TextWatcher {
      override fun afterTextChanged(s: Editable?) {
               search(s.toString())
      }

      override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
      }

      override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
      }
    })

That's all. Now you can search over the recyclerView easily and smoothly.

Hope you have learnt something new. Thanks for reading.

Keep learning! ๐Ÿ”ฅ

Did you find this article valuable?

Support LearnDroid by becoming a sponsor. Any amount is appreciated!

ย