1

I am trying to implement a SearchView in my room with a view SQLite Database. The app has a RecyclerView with a CardView that displays rows from the database. I am trying to make it so a user can use a SearchView to filter the CardView and only show cards matching the search. I have been following the information in this post: Android - Implementing search filter to a RecyclerView .

Anyway, my app currently uses a LiveData method getAllFlares() and it returns all of the rows from the database. The return is of course of type LiveData. The method of filtering in the link has you pass an ArrayList as an argument to a filtering method. So I was trying to pass the results of getAllFlares() to the filtering method. To make the types match up, I altered the filtering method to accept a LiveData list instead of an ArrayList. Here is the code where I am trying to pass the LiveData list to the filtering method:

LiveData<List<Flare>> flare;
flare = mFlareViewModel.getAllFlares();
adapter.filterFlareList(flare);

But it gives this error like:

updateFlareList (java.util.List<com.android.fibrnah.Flare>) in FlareListAdapter cannot be applied to (androidx.lifecycle.LiveData<java.util.List<com.android.fibrnah.Flare>>)

What am I doing wrong? I feel that there is some fundamental flaw in what I am doing with LiveData.

Dinesh Shingadiya
  • 988
  • 1
  • 8
  • 23
Jarf
  • 61
  • 1
  • 7
  • why not to filter on the room/sqlite level? i mean: do not `select * from table` but instead `select * from table where your_condition_here` – pskink May 15 '19 at 05:07
  • @pskink filtering the already displayed cards seemed simpler than pulling the info from the database, hiding the current cardview, and creating a new cardview with the info from the database – Jarf May 15 '19 at 05:19
  • you dont have to hide anything: just make one `getFlares(String filter)` method that handles both cases (all the data and filtered data) – pskink May 15 '19 at 05:25
  • the error you get is because your updateFlareList function needs simple java list but you are passing a livedata object. One way is to make a simple java list from livedata. But that would be very much costly. The better option is to get only the needed data from the room db, i.e. applying filter on room itself in the query. – Abdullah Riaz May 15 '19 at 05:39
  • Thanks for the advice pskink and @Abdullah Riaz, I'll start working at it from that angle – Jarf May 15 '19 at 06:07
  • its as simple as: `@Query("SELECT * FROM cheese where name like :constraint") DataSource.Factory filter(String constraint);` – pskink May 15 '19 at 06:10
  • Have you found a solution? I am wondering the same – IgorGanapolsky Feb 19 '20 at 15:03

0 Answers0