0

I have a MainActivity that shows a Fragment containing a RecyclerView. I am loading data from SQL using an Asynctask, all the data shows perfectly.

What I want to know is, I've got a search icon in my toolbar inflated in the MainActivity, when the user clicks on it and enters data, it should filter through the RecyclerView in the Fragment.

I have already implemented SearchView.OnQueryTextListener in the MainActivity, I've also implemented methods onQueryTextChange() and onQueryTextSubmit(). I just don't know how to filter the RecyclerView in the Fragment directly using the search in the MainActivity.

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
Derrick Feehi
  • 63
  • 1
  • 5
  • Once the results are filtered you need to update the adapter list and call `notifyDatasetChanged()` – Chisko Apr 06 '18 at 18:47

1 Answers1

1

Create a method in your Adapter Class and implement it like this

To get your Recyclerview's Adapter reference in MainActivity or Host Activity you can ( but not recommended ) do like this:

RecyclerView recylerView = findViewById(R.id.my_recycler_view);
MyAdapter myAdapter = recyclerView.getAdapter();

Then in your onQueryTextChange() and onQueryTextSubmit() method do :

myAdapter.filter(queryText);
Balraj
  • 328
  • 2
  • 7