Searching Room Database with search Widget

I finished the exploring android book and have been trying to add some features to the app that we built.
I would like to search the room database and have the recycler view update my items as I type in the search bar.

I have been at it for 4 days and can not figure out how to do this.

I have my @query and search widget set up correctly, just not sure what to do when my search function is called by onQueryTextChange. I tried to do something similar to the way the completed and outstanding filters were implemented but no luck.

If someone could point me in the right direction that would be great.

Elements of Android Room has its FTS series of samples, starting with FTS · v0.3 · Mark Murphy / cw-room · GitLab. These implement a search, in that case using SQLite’s full-text search capability. While you might not be needing full-text search, you could see how that sample approaches searching and presenting search results. There, I happened to use a separate fragment for the search results.

In general, and off the cuff, you could:

  • Have the fragment pass the search expression to the viewmodel
  • Have the viewmodel hold onto the search expression, in case you need it back after a configuration change
  • Have the viewmodel call a function on your repository to get the results based on the current search expression and filter
  • Have the viewmodel emit the revised roster to display, using the same technique that is used for the filter-only results

Thanks Mark I’ll give it another go and see how I get on.