Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

kotlin

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<*>) {}
}
like image 442
AMEL BENAIDA Avatar asked Mar 18 '19 10:03

AMEL BENAIDA


People also ask

What is ArrayAdapter in Kotlin?

android.widget.ArrayAdapter. You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .

What does an ArrayAdapter do?

ArrayAdapter is an Android SDK class for adapting an array of objects as a datasource. Adapters are used by Android to treat a result set uniformly whether it's from a database, file, or in-memory objects so that it can be displayed in a UI element. The ArrayAdapter is useful for the latter.

What is simple_list_item_1?

simple_list_item_1 , (a layout built into Android that provides standard appearance for text in a list), and an ArrayList called restaurants .


2 Answers

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)
like image 68
Hemant Parmar Avatar answered Sep 20 '22 21:09

Hemant Parmar


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())
like image 37
Son Truong Avatar answered Sep 17 '22 21:09

Son Truong