简体   繁体   中英

How to override OnItemSelected method in Fragment

I'm trying to override the OnItemSelected method of a Spinner in my Fragment , the same way I did with OnClick on Button , but it's not responding anymore

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 this way it is getting very polluted.

Another question: If possible , how this OnNothingSelected method works and how to use it more effectively?

In the expression "if (itemSelected! =" ... ")" is because the first element of my spinner is a string with "....", so as not to have any item selected at the beginning, I'm using this wonderfull technique... How could I improve 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>

Many thanks in advance!

EDIT: Reformatted code for better visualization, but the problem persists

As the doc says, if you have multiple spinners, you should judge the id of parent like below, sorry,I just know java.

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        int iid = parent.getId();
        switch (iid) {
            case R.id.spinner1:
                Toast.makeText(getActivity(), "hello,spinner1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.spinner2:
                Toast.makeText(getActivity(), "hello,spinner2", Toast.LENGTH_SHORT).show();
                break;
            break; 
            default:
                break;
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM