如何将 ArrayAdapter 与 Spinner Kotlin 一起使用

How to use an ArrayAdapter with Spinner Kotlin

我正在尝试在 AndroidStudio (Kotlin) 中创建下拉列表 (Spinner)。

所以,我在 Activity 上创建了 convert_from_spinner。然后我尝试向列表中添加值,但是 IDE 给我一个错误:

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)
    }

和错误

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

我试过使用 'activity' 或 'getActivity',因为我听说

但它也不起作用:

您必须在 onCreateView 中而不是在 onCreate 中使用此方法

确保您的 Fragment 导入是

import androidx.fragment.app.Fragment

    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.<your spinner_id>)
    convert_from_spinner.adapter = ArrayAdapter<String>(ac, android.R.layout.simple_list_item_1, data)

您应该使用 requireContext()requireActivity() 而不是 thisactivity

activity 不同,

Fragment 不是从 Context class 继承的,这意味着它不能作为上下文传递到参数中。以上 2 种方法只是提供了在任何 Fragment

中获取上下文的机会