1

I'm new in development and I'm aware that stack isn't for 'full code requests'. But I'm stuck and can't find the solution.

I'm using Room database which has two columns - firstName and lastName. I'm loading the database with passed firstName parameter:

@Query("SELECT * FROM passenger WHERE firstName LIKE :firstName")
List<Passenger>getAllByName (String firstName);

It works as supposed.

But.. When I want to update Passenger, I need to populate data again, again, and again. There comes LiveData and observer.

But.. setValue in LiveData is private and I cannot send any parameters for Query line. There comes MutableLiveData, but how can I implement that?

476rick
  • 2,764
  • 4
  • 29
  • 49
Eduardas Šlutas
  • 178
  • 3
  • 13

1 Answers1

-1

@Eduardas Seems like you need to return LiveData instead:

LiveData<List<Passenger>> getAllByName (String name);

And you can write a transformation in the ViewModel or you can directly observe it from your Activity/Fragment.

In activity/fragment onCreate() or onResume():

YourDao.getAllByName(name).observe(this, new LiveData<List<Passenger>>(){
   @Override
   public void onChanged( @Nullable List<Passenger>) {
     // update your adapter if the list isn't null
   }
});

Something similar to above. You can add customization as per your use case.

theThapa
  • 581
  • 4
  • 11