简体   繁体   中英

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

I have a spinner where I want to display a list of strings (for example languages); I have tried many ways but none worked. And now I am stuck with this only error. It's about ArrayAdapted, "None of the following functions can be called with the arguments supplied". I tried the trick mentioned in this post Link to a similar question .

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

and this:

val arrayAdapter = ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, statuts.toList())

and this:

val arrayAdapter = ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, statuts)

But it's not working. Please help me, I am new to Kotlin.

    class FirstFragment : Fragment(),AdapterView.OnItemSelectedListener {

    // attributes
    var statuts = arrayOf("English", "French", "Spanish")
    var spinner:Spinner? = null
    var textView_msg:TextView? = null

    // methods    
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        textView_msg = msg
        spinner = this.statuts_sp
        spinner!!.setOnItemSelectedListener(this)
        val arrayAdapter = ArrayAdapter(this,android.R.layout.simple_spinner_item, statuts.toList())
        // Set layout to use when the list of choices appear
        arrayAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        // Set Adapter to Spinner
        statuts_sp!!.adapter = aa
        // Inflate the layout for this fragment
        return inflater!!.inflate(R.layout.fragment_first, container, false)
    }
    /*************************************************************************************/
    override fun onItemSelected(arg0: AdapterView<*>, arg1: View, position: Int, id: Long) {
    textView_msg!!.text = "Selected : "+statuts[position]
         }
    /*************************************************************************************/
    override fun onNothingSelected(arg0: AdapterView<*>) {}
}

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

Problem: ArrayAdapter (Context context, int resource, List objects) receives a Context as first parameter, but you pass this as Fragment (not a subclass of Context ) as first parameter.

Solution: Change your code from

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

to

val aa = ArrayAdapter<String>(requireActivity(), R.layout.simple_spinner_item, statuts.toList())

android:entries="@array/product_order_list" 添加到 xml 中的微调标签

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM