I have a LazyColumn connected to a Room database query. I'm expanding on the "Room With a View" sample (https://developer.android.com/codelabs/android-room-with-a-view-kotlin#0), but I'm using LazyColumn instead of a RecyclerView to display a list of table entries.
After inserting an entry (Word), I want the LazyColumn to scroll automatically to the row with the newly inserted entry (or at least make it visible). Note that the list query is sorted alphabetically, the new row will not appear at the end of the list.
// table:
@Entity(tableName = "word_table")
class Word(
@PrimaryKey @ColumnInfo(name = "word") val text: String
)
// DAO:
@Query("SELECT * FROM word_table ORDER BY word COLLATE NOCASE ASC")
fun getAlphabetizedWords(): Flow<List<Word>>
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(word: Word)
// repository:
val allWords: Flow<List<Word>> = wordDao.getAlphabetizedWords()
suspend fun insert(word: Word) {
wordDao.insert(word)
}
// viewModel:
val allWords: LiveData<List<Word>> = repository.allWords.asLiveData()
fun insert(word: Word) = viewModelScope.launch {
repository.insert(word)
}
// simplified composable:
@Composable
fun WordList() {
val list by mWordViewModel.allWords.observeAsState(listOf())
LazyColumn() {
items(list) { word ->
Row() {
Text(word.text)
}
}
}
}
Otherwise everything is working fine, the repository and view model are implemented following general guidelines. Any ideas?