How to use an ArrayAdapter with Spinner Kotlin

BlueScreen

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

BlueScreen

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.

modifier le
0

laisse moi dire quelques mots

0commentaires
connexionAprès avoir participé à la revue

Articles connexes

Comment utiliser un ArrayAdapter avec Spinner Kotlin

Spinner non rempli avec ArrayAdapter

ArrayAdapter avec spinner dans kotlin - Aucune des fonctions suivantes ne peut être appelée avec les arguments fournis

ArrayAdapter.clear Kotlin

ArrayAdapter utilise Kotlin Android

Kotlin Spinner OnItemSelected Intent

Comment changer mon spinner ArrayAdapter <String> en hashmap?

How to use a predefined lambda in kotlin?

How to use a swift library in kotlin native?

Android Kotlin Get Value of Selected Spinner Item

Use string array to populate Spinner

Comment appeler la méthode ArrayAdapter.addAll dans Kotlin?

Comment utiliser un ArrayAdapter dans un fragment avec Kotlin

Mon ArrayAdapter me donne une valeur nulle - Kotlin

Erreur: ';' nouveau ArrayAdapter attendu <> (contexte: this, android.R.layout.simple_spinner_item, cours)

How to implement search function on custom ArrayAdapter with searchview?

How do I use an enum within a kotlin data class

How to use suspend modifier in Room Dao after kotlin upgrade to 1.6.0?

how to use spring annotations like @Autowired or @Value in kotlin for primitive types?

How to use the same RecyclerView adapter with Activity and Fragment in Kotlin?

Kotlin on Android: How to use LiveData from a database in a fragment?

créer un adaptateur spinner personnalisé dans kotlin

Le spinner ne fonctionne pas dans mon fragment Kotlin

Spinner dans le fragment de navigation Kotlin Jetpack

Comment kotlin getposition spinner par valeur avec un objet personnalisé?

Spinner Android ne se remplit pas | Android Kotlin

Spinner n'affiche pas les options dans le fragment kotlin

Je ne peux pas utiliser correctement la fonction spinner [kotlin]

Spinner ne fonctionne pas dans Fragment sur Kotlin

TOP liste

  1. 1

    comment afficher un bouton au-dessus d'un autre élément ?

  2. 2

    impossible d'obtenir l'image d'arrière-plan en plein écran dans reactjs

  3. 3

    Je continue à obtenir l'objet 'WSGIRequest' n'a pas d'attribut 'Get' sur django

  4. 4

    comment supprimer "compte de connexion google" à des fins de développement - actions sur google

  5. 5

    Conversion double en BigDecimal en Java

  6. 6

    Impossible d'accéder à la vue personnalisée pendant le test de l'interface utilisateur dans XCode

  7. 7

    Algorithme: diviser de manière optimale une chaîne en 3 sous-chaînes

  8. 8

    Passer la taille d'un tableau 2D à une fonction ?

  9. 9

    Comment obtenir l'intégration contextuelle d'une phrase dans une phrase à l'aide de BERT ?

  10. 10

    Comment changer le navigateur par défaut en Microsoft Edge pour Jupyter Notebook sous Windows 10 ?

  11. 11

    CSS: before ne fonctionne pas sur certains éléments,: after fonctionne très bien

  12. 12

    Comment créer un bot à compte à rebours dans Discord en utilisant Python

  13. 13

    Comment ajouter une entrée à une table de base de données pour une combinaison de deux tables

  14. 14

    Exporter la table de l'arborescence vers CSV avec mise en forme

  15. 15

    Comment activer le message Pylint "too-many-locals" dans VS Code?

  16. 16

    Créer un système Buzzer à l'aide de python

  17. 17

    Spring @RequestParam DateTime format comme ISO 8601 Date Heure facultative

  18. 18

    Empêcher l'allocation de mémoire dans la génération de combinaison récursive

  19. 19

    Déplacement des moindres carrés d'ajustement pour les déplacements de points ayant des problèmes

  20. 20

    Comment choisir le nombre de fragments et de répliques Elasticsearch

  21. 21

    Microsoft.WebApplication.targets

chaudétiquette

Archive