1

I am trying to set the options of a Spinner's drop-down menu, which requires taking in an Array of options as the last parameter. However, when I run my code, I get the following error:

java.util.HashMap$KeySet cannot be cast to java.lang.String[]

Below is my code:

var ringtoneNamesToURIs: MutableMap<String, String> = getRingtoneList()
var ringtoneNames: Array<String> = ringtoneNamesToURIs.keys as Array<String>

val ringtoneSpinner = root.findViewById(R.id.ringtoneSpinner) as Spinner
val ringtoneDropdown: ArrayAdapter<String>? = context?.let {
    ArrayAdapter<String>(
        it,
        android.R.layout.simple_spinner_item,
        ringtoneNames
    )
}
1
  • You should consider using a List rather than an Array. It's more Kotlinic and also works with Android's ArrayAdapter. Commented May 7, 2021 at 11:11

1 Answer 1

1

The keys in a map are saved in a Set, not in an array. So you can not just cast. You have to covert the keys set to an array.

val ringtoneNames:Array<String> = ringtoneNamesToURIs.keys.toTypedArray() 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.