This is part of an online Android development tutorial. This lesson does not use any ViewModel or repository. Room can be used to add database support to your application without doing low-level operations directly with SQLite.
Find the source code at https://github.com/djnotes/phonebook
Solution to the bug shown in the video:
Since we are updating the database in GlobalScope.launch{}, which means background, we must update the UI inside there. But, since it is background process, we need to run UI-update code inside runOnUiThread. The correct code for updateList() is:
Since we are updating the database in GlobalScope.launch{}, which means background, we must update the UI inside there. But, since it is background process, we need to run UI-update code inside runOnUiThread. The correct code for updateList() is:
private fun updateList() {
GlobalScope.launch{
mContacts = appDb.contactDao().getAll() as MutableList<Contact>
runOnUiThread{
mAdapter = ListAdapter(this@MainActivity, mContacts)
mList.layoutManager = LinearLayoutManager(this@MainActivity, LinearLayoutManager.VERTICAL, false)
mList.adapter = mAdapter
mAdapter.clickListener = this@MainActivity
}
}
}
Enjoy! Let me know what you think of the tutorials.