0
I’m trying to overwrite the method Onitemselected of a Spinner in my Fragment, the same way I did with the Button, but it’s not working... how could I get the same result?
Inside Oncreate, the commented block works perfectly, but I want to "clean" it, because I have more than one spinner in this layout and this way is getting very polluted.
Another question: If possible, could someone explain to me how this method works Onnothingselected and how to use it more effectively?
In the expression "if (itemSelected != ". . .")" is because the first element of my spinner is a string with ". . ." , to not leave any item already selected from the beginning, has some better way to solve this?
My code is this:
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_veteran2.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, from now on!
EDIT: I edited the code for better visualization, but the problem persists
I made the change, but it still didn’t work...
– MTSA