I'm trying to create dropdown list (Spinner) in AndroidStudio (Kotlin).
So, I create convert_from_spinner
on my Activity. Then I tried to add values to the list, but IDE gives me an error:
package com.currency_converter.ui.home
import android.app.Activity
import android.content.ClipData
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.currency_converter.R
import kotlinx.android.synthetic.main.fragment_home.*
import android.content.ClipboardManager
import android.content.Context
import android.text.TextWatcher
import android.widget.ArrayAdapter
import android.widget.Spinner
import androidx.core.content.ContextCompat.getSystemService
//
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
val values : Array<String> = arrayOf("USD", "UAH", "GBD", "EUR", "BIT", "RUB")
var data = ArrayList<String>()
data.add("USD")
data.add("RUB")
val convert_from_spinner: Spinner = root.findViewById(R.id.convert_from_spinner)
convert_from_spinner.adapter = ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, data)
return root
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val values : Array<String> = arrayOf("USD", "UAH", "GBD", "EUR", "BIT", "RUB")
var data = ArrayList<String>()
data.add("USD")
data.add("RUB")
convert_from_spinner.adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data)
}
And error
None of the following functions can be called with the arguments supplied.
<init>(Context, Int, Array<(out) TypeVariable(T)!>)
where T = TypeVariable(T) for
constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, objects: Array<(out) T!>) defined in android.widget.ArrayAdapter
<init>(Context, Int, Int)
where T = TypeVariable(T) for
constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, textViewResourceId: Int) defined in android.widget.ArrayAdapter
<init>(Context, Int, (Mutable)List<TypeVariable(T)!>)
where T = TypeVariable(T) for
constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, objects: (Mutable)List<T!>) defined in android.widget.ArrayAdapter Alt+Shift+Enter Alt+Enter
I've tried to use 'activity' or 'getActivity' as I heard here
But it doesn't workin too:
You should use requireContext()
or requireActivity()
instead of this
or activity
.
Fragment
, unlike activity
, is not inherited from the Context class
, which means it cannot be passed into arguments as a context. The above 2 methods just give the opportunity to get the context inside any Fragment
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots