When I click on the selected spinner my data does not appear (space appears) - Kotlin

71 views Asked by At

I have a spinner in my app and when I click it, the related data (my string file) comes up empty.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    ArrayAdapter.createFromResource(requireActivity(),R.array.cityList,android.R.layout.simple_spinner_item).also { adapter ->
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinner_city.adapter = adapter
    }

override fun onItemSelected(parent: AdapterView<*>?, p1: View?, position: Int, p3: Long) {
    val selectedCity = parent!!.getItemAtPosition(position).toString()
    Log.i("Item selected?: ", selectedCity)

    spinner_city.onItemSelectedListener = this

}
override fun onNothingSelected(p0: AdapterView<*>?) { }

my xml.

<Spinner
            android:id="@+id/spinnerCity"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="32dp"
            android:background="@drawable/spinner_stayle"
            android:gravity="start"
            android:textColorHint="@color/black"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/surnameText"
            android:spinnerMode="dropdown"
            android:prompt="@string/selected_city"
            android:entries="@array/cityList"/>

api ss

enter image description here

1

There are 1 answers

0
mahdi talaee On

This is Simple Implementation of Spinner

class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener  {
    var courses = arrayOf(
        "C", "Data structures",
        "Interview prep", "Algorithms",
        "DSA with java", "OS"
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val spino = findViewById<Spinner>(R.id.spinnerCity)
        spino.onItemSelectedListener = this

        val ad: ArrayAdapter<String> = ArrayAdapter(
            this,
            android.R.layout.simple_spinner_item,
            courses
        )

        ad.setDropDownViewResource(
            android.R.layout.simple_spinner_item
        )

        spino.adapter = ad
    }

    override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
       Log.d("Item selected?: ", courses[p2])
    }

    override fun onNothingSelected(p0: AdapterView<*>?) {
    }
}

And This is xml of this

<Spinner
        android:id="@+id/spinnerCity"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:gravity="start"
        android:spinnerMode="dropdown"
        android:textColorHint="@color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />