1

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

enter image description here

I've tried to use 'activity' or 'getActivity' as I heard here

But it doesn't workin too:

enter image description here

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

Comments

4

You will have to use this method in onCreateView not in onCreate

Make sure your Fragment import is

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)

8 Comments

Type mismatch. Required: Context Found: FragmentActivity? I've tried it to. but not working.
And this ``` val activity_var: Activity? = activity convert_from_spinner.adapter = ArrayAdapter<String>(activity_var, android.R.layout.simple_list_item_1, data) ```is not working too
I tried to do this in onCreate. onViewCreated onCreateView, but in no function it didn't work
Updated the answer just paste it in your onCreateView
No. The same problem: Waiting Contex. Geted Activity? imgur.com/a/6d6queO
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.