Home > database >  Type Error when adding list to adapter - Android
Type Error when adding list to adapter - Android

Time:09-30

I am trying to add a list to my adapter but got the following error

private val repository = Repository()
private var songResources = repository.fetchData()
private var songList = songResources.keys.toMutableList()

adapter.submitList(songList)

Error Type mismatch: inferred type is MutableList<String> but (Mutable)List<SongInfo!>? was expected

Here is my Repository.kt

class Repository {
    private var songResources = hashMapOf(
        "Dark Star" to
                SongInfo("Dark Star",
                    R.raw.dark_star_excerpt,
                    "1:30"),
        "What's Mine" to
                SongInfo("What's Mine",
                    R.raw.whats_mine_excerpt,
                    "1:15")
    )
    fun fetchData(): HashMap<String, SongInfo> {
        return songResources
    }

I checked using my IDE by printing songList and got. [Dark Star, What's Mine] Which I wanted.

What did I do wrong?

CodePudding user response:

Change

private var songList = songResources.keys.toMutableList()

to

private var songList = songResources.values.toMutableList()
  • Related