Sagar Balyan
Sagar Balyan

Reputation: 658

Update UI With Live Data & Not MutableLiveData - Room Database

So i have this Dao where i have declared a LiveData Method.

@Dao
public interface EarthquakeDao {

    @Insert
    public void insert(Properties properties);

    @Query("select * from properties")
    public LiveData<List<Properties>> getAllProperties();

}

I have a sortByTitle method in my ViewModel.

    public void sortByTitle(){
            Collections.sort(propertiesList.getValue(), new Comparator<Properties>() {
                @Override
                public int compare(Properties o1, Properties o2) {
                    return o1.title.compareTo(o2.title);

                }
            });
            propertiesList.postValue(featuresList.getValue());
    }

Since the propertiesList is LiveData rather than MutableLiveData then i am not able to update the UI by using the postValue method.

enter image description here

In order to update the UI, i need to use MutableLiveData, and on the other hand...my database gives me a LiveData Object. So i am unable to achieve this.

Also, i cant make Dao's method a MutableLiveData, i know that.

Then how do i achieve this?

Upvotes: 1

Views: 467

Answers (1)

R&#243;bert Nagy
R&#243;bert Nagy

Reputation: 7670

You could simply map the LiveData coming from Room. For example:

public LiveData<List<Properties>> properties = Transformations.map(propertiesList, list -> {
  return sort(list);
})

See Transformations.map for more information

Upvotes: 1

Related Questions