I took up for learning implementing MVVM from Google Guide here:
https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#8 (posted link especially to page I'm interested in).
Since I understood implementing it in Java, I decided to switch to Kotlin.
While initializing constructor in class extending AndroidViewModel
I need to call super
and it throws me following error:
"super' is not an expression it can only be used on the left-hand side of a dot ('.')"
As I googled and found similar topic but I haven't understood it at all so I didn't solve my problem.
This my code for ViewModel
class:
class NotesViewModel private constructor(application: Application) : AndroidViewModel(application){
var mRepository: NotesRepository? = null
var mAllNotes: LiveData<List<Notes>>? = null
init {
super(application) // <-- here it throws me an error
mRepository = NotesRepository(application)
mAllNotes = mRepository!!.getAllWords()
}
fun getAllNotes(): LiveData<List<Notes>>{
return mAllNotes!!
}
fun insert(notes: Notes){
mRepository!!.insert(notes)
}
}
So, how should I properly call super, construct a constructor? This is proper java code for this class:
public class WordViewModel extends AndroidViewModel {
private WordRepository mRepository;
private LiveData<List<Word>> mAllWords;
public WordViewModel(Application application) {
super(application);
mRepository = new WordRepository(application);
mAllWords = mRepository.getAllWords();
}
LiveData<List<Word>> getAllWords() {
return mAllWords;
}
void insert(Word word) {
mRepository.insert(word);
}
}