0

I have used Android's spinner for static values by setting string-array in XML resources. This works without issue.

However, for a specific spinner, I need to set data dynamically based on API returns. While I have set the returned values into an array of strings, when I send this to the adapter, it expects type Int (which a returned string-array from XML translates into).

I'm not sure how to make these arrays work within Kotlin to be received by the spinner.

val managerSpinner: Spinner = binding.spinnerManager
        val managerList: MutableList<String> = mutableListOf()
        var managerArr: Array<String>

        viewModel.managers.observe(viewLifecycleOwner, {
            if (null != it) {
                it.items?.forEach { manager ->
                    managerList.add(manager.fullname!!)
                }
                managerArr = managerList.toTypedArray()
                ArrayAdapter.createFromResource(
                        activity?.applicationContext!!,
                        managerArr!!,
                        android.R.layout.simple_spinner_item
                ).also { adapter ->
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                    managerSpinner.adapter = adapter
                }
            }
        })

1 Answer 1

2

how about like this:

  1. it use ArrayAdapter constructor rather than using createFromResource function factory
  2. use addAll function to add item
val managerSpinner: Spinner = binding.spinnerManager
        val managerList: MutableList<String> = mutableListOf()
        var managerArr: Array<String>

        viewModel.managers.observe(viewLifecycleOwner, {
            if (null != it) {
                it.items?.forEach { manager ->
                    managerList.add(manager.fullname!!)
                }
                managerArr = managerList.toTypedArray()
                ArrayAdapter(
                        activity?.applicationContext!!,
                        android.R.layout.simple_spinner_item
                ).also { adapter ->
                    adapter.addAll(managerArr)
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                    managerSpinner.adapter = adapter
                }
            }
        })

Sign up to request clarification or add additional context in comments.

2 Comments

Initially, this didn't work because it wouldn't consume the array. So I attempted this while just using the MutableList, and that allows for it to set. Thanks for guiding me to the right answer!
Also, I had to add a type of String to ArrayAdapter for it to work properly, but those are just minor fixes.

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.