Home > Blockchain >  Spinner is not working in Fragment on Kotlin
Spinner is not working in Fragment on Kotlin

Time:05-25

The spinner just doesn't work, I tried different versions of the code, but it didn't work in any of them

Can anyone help solve this problem?

TransferFragment.kt

    package com.example.hotel2.transfer
    
    class TransferFragment : Fragment(){
    
        private var _binding: FragmentTransferBinding? = null
        private val binding get() = _binding!!
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            // Inflate the layout for this fragment
            _binding = FragmentTransferBinding.inflate(inflater, container, false)
            val view = binding.root
            return view
    
            val transfers = arrayOf<String>("Flowers", "Candies")
            val adapter: ArrayAdapter<String> =
                ArrayAdapter<String>(activity?.applicationContext!!, android.R.layout.simple_spinner_item, transfers)
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            val spinner = binding.spinner
            spinner.adapter = adapter
            spinner.prompt = "Title"
            spinner.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
                override fun onItemSelected(
                    parent: AdapterView<*>?, view: View?,
                    position: Int, id: Long
                ) {
    
                }
    
                override fun onNothingSelected(arg0: AdapterView<*>?) {}
            })
    
        }
    
        override fun onDestroyView() {
            super.onDestroyView()
            _binding = null
        }
    }

fragment_transfer.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".transfer.TransferFragment">
    
        <Spinner
            android:id="@ id/spinner"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="300dp"
            android:layout_marginStart="50dp"></Spinner>
    
    </androidx.constraintlayout.widget.ConstraintLayout>

According to my guess, the problem is in activity?.applicationContext!!, but I do not understand how to solve it.

CodePudding user response:

In onCreateView() you have this statement as the 4th statement in the method:

return view

All the code after this statement doesn't get executed.

Interestingly enough, your IDE (Android Studio or whatever) should tell you that!

  • Related