'AlertDialog setItems with a list from room database: getValue() returns null
I'm simply trying to select from a list of Artists in my room database in an AlertDialog. Calling getValue() on the LiveData object from the viewModel consistently gives me null. Do I really need to make a ListAdapter for something this simple?! Why is it so hard to get some strings from the database?
ArtistDao
@Dao
interface ArtistDao {
@Query("SELECT * FROM artist_table")
fun getAllArtists(): Flow<List<Artist>>
@Query("SELECT name FROM artist_table")
fun getArtistList(): Flow<List<String>>
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(artist: Artist)
}
Menu Selection Option in Main Activity
R.id.action_choose_artist -> {
//create artist list
val testList = songViewModel.artists.value
val artistList = testList?.toTypedArray()
// User chose choose artist action
val alertDialog: AlertDialog? = this.let {
val builder = AlertDialog.Builder(it)
builder.apply {
setTitle(R.string.choose_artist)
setItems(artistList, DialogInterface.OnClickListener { dialog, which ->
// The 'which' argument contains the index position
// of the selected item
artistName = artistList!![which]
})
}
// Create the AlertDialog
builder.create()
}
alertDialog?.show()
true
}
SongViewModel
val artists: LiveData<List<String>> = repository.artists.asLiveData()
SongRepository
val artists: Flow<List<String>> = artistDao.getArtistList()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
