ArrayAdapter with spinner in kotlin - None of the following functions can be called with the arguments supplied

While using Array Adapter in fragment, first parameter must be current class context which comes from activity.

Replace this line

val aa = ArrayAdapter(this,android.R.layout.simple_spinner_item, statuts.toList())

To :

var list = ArrayList<String>()
val aa = ArrayAdapter(activity,android.R.layout.simple_spinner_item,list)

try this :

in your main activity :

    import android.content.Intent
    import android.os.Bundle
    import android.support.design.widget.Snackbar
    import android.support.v7.app.AppCompatActivity;
    import android.view.View
    import android.widget.ArrayAdapter
    import android.widget.Toast
    import kotlinx.android.synthetic.main.activity_logged.*
    import kotlinx.android.synthetic.main.content_logged.*
    import kotlinx.android.synthetic.main.content_main.*

    class logged : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_logged)
        setSupportActionBar(toolbar)
        // Create an ArrayAdapter
        val adapter = ArrayAdapter.createFromResource(this,
            R.array.city_list, android.R.layout.simple_spinner_item)
        // Specify the layout to use when the list of choices appears
       adapter.setDropDownViewResource(
        android.R.layout.simple_spinner_dropdown_item)
        // Apply the adapter to the spinner
        spinner.adapter = adapter
        }

    fun getValues(view: View) {
        Toast.makeText(this, "Spinner 1 " + spinner.selectedItem.toString()
                , Toast.LENGTH_LONG).show()
    }

    }

I put the list of item in strings.xml

<string-array name="city_list">
    <item>Bangkok</item>
    <item>London</item>
    <item>Paris</item>
    <item>Singapore</item>
    <item>New York</item>
    <item>Istanbul</item>
    <item>Dubai</item>
    <item>Kuala Lumpur</item>
    <item>Hong Kong</item>
    <item>Barcelona</item>
</string-array>

Check this,

 val countryAdapter = ArrayAdapter<Country>(
            this, R.layout.dialog_row_place, countryArrayList
        )
        countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mListView.setAdapter(countryAdapter)

Layout dialog_row_place is a custom xml file for the List item

Tags:

Android

Kotlin