4

Firstly i might not actually be understanding what mutable live data is correctly.

i get data from room using live data and then show it to the UI, it is a question with answers, so at the end of the question i would like to update the database with correct answers, the time it took etc etc.

I cannot work out how to use Mutable live data as there is next to no useful information on it or i'm incredibly stupid!

So firstly, can i actually update the database with mutable live data?

if so how? (i don't like asking this but i am a really stumped)

Dao

@Query("SELECT * FROM question_table WHERE :id = uoe_id")
LiveData<Question> getQuestionLiveData(int id);

Repo

public LiveData<Question> getQuestionLiveData(int id) {
  return questionDao.getQuestionLiveData(id);
}

ViewModel

public LiveData<Question> getQuestionLiveData(int id) {
  return questionRepository.getQuestionLiveData(id);
}

And then observing it in the View

viewModel.getQuestionLiveData(packageId).observe(getViewLifecycleOwner(), new Observer<com.questionTest.practice.Model.Question>() {
            @Override
            public void onChanged(com.questionTest.practice.Model.QuestionQuestion question) {

   Do stuff here////

                }
            }
        });

The next part is where i'm not sure. i added this in the view model

MutableLiveData mutableLiveData = new MutableLiveData();

and then tried to assign the this to the question

mutableLiveData = (MutableLivedata) getQuestionLiveData(id);

so i could use update the values but this throws an Casting error.

Im either missing something or i cannot do this so any help will be welcomed thanks

Dave
  • 313
  • 1
  • 3
  • 16
  • Also new to this whole livedata thing. I think maybe look into Room? I haven't tried it myself, but maybe it is what you are looking for? – Zee May 03 '19 at 11:57

1 Answers1

-1

In this case, There is no need for MutableLiveData. MutableLiveData is LiveData which publicly exposes setValue() and postValue() method. So if you are not setting LiveData values inside ViewModel class, there is no need for MutableLiveData. Here the Dao class generate LiveData for you and you should observe that LiveData.

Boken
  • 4,825
  • 10
  • 32
  • 42
Sourav Bagchi
  • 656
  • 7
  • 13