I'm trying to overwrite the OnItemSelected method of a Spinner in my Fragment , just as I did with the Button , but it is not working ... How would I achieve the same result?
Inside the OnCreate, the commented block works perfectly, but I want to clean it because I have more than one spinner in this layout and it's getting very polluted.
Another question : If possible, could someone explain to me how this OnNothingSelected method works and how to use it more effectively?
In the expression " if (itemSelected!=". ") " is why the first element of my spinner is a string with "...", so that no items are already selected is there any better way to solve this?
My code is as follows:
Vetarano2.kt
com.mtsa.escudeiro_rpghelper.fragments
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.Button
import android.widget.Spinner
import android.widget.Toast
import com.mtsa.escudeiro_rpghelper.R
class Veterano2 : Fragment(), View.OnClickListener, AdapterView.OnItemSelectedListener {
private lateinit var spinner: Spinner
private lateinit var button: Button
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val fragView = inflater.inflate(R.layout.fragment_veterano2, container, false)
initViews(fragView)
initListeners()
// SPINNER RAÇA
/*
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
val selecionado = parent.getItemAtPosition(position) as String
Toast.makeText(context, "Opção escolhida: $selecionado", Toast.LENGTH_SHORT).show()
}
override fun onNothingSelected(parent: AdapterView<*>) {}
}
*/
return fragView
}
private fun initViews(v: View) {
spinner = v.findViewById(R.id.spinner2)
button = v.findViewById(R.id.button2)
}
private fun initListeners() {
spinner.onItemSelectedListener = this
button.setOnClickListener(this)
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
when (view?.id) {
R.id.spinner2 -> {
val selecionado = parent?.getItemAtPosition(position) as String
Toast.makeText(context, "Opção escolhida: $selecionado", Toast.LENGTH_SHORT).show()
}
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.button2 -> {
Toast.makeText(context, "SALVAR", Toast.LENGTH_SHORT).show()
}
}
}
}
fragment_veterano2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="64dp">
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/array_example"
android:spinnerMode="dropdown" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="Button" />
</LinearLayout>
arrays_diversos.xml
<resources>
<string-array name="array_example">
<item>AAAAA</item>
<item>BBBBB</item>
<item>CCCCC</item>
<item>DDDDD</item>
<item>EEEEE</item>
</string-array>
</resources>
Thank you in advance!
EDIT: I edited the code for better visualization, but the problem persists