Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner onItemSelected() not being called

Tags:

android

kotlin

I've implemented an ArrayAdapter to populate my Spinner view. The Spinner is working fine, however android is not detecting when I click an item in the spinner.

I've abided by all the requirements in the spinner example in the Android docs including implementing AdapterView.OnItemSelectedListener to my Activity and overriding it's two methods OnItemSelectedListener and onNothingSelected, however, none of my Log statements in those methods print so they are not being called.

I've also set the listener to my spinner via choose_user.onItemSelectedListener = this@PlayerDetails.

Here's my activity:

class PlayerDetails : AppCompatActivity(), View.OnClickListener, TextWatcher, AdapterView.OnItemSelectedListener {
    val TAG: String = "PlayerDetails"
    val FirebaseTAG: String = "FirebaseDebug"

    var numOfPlayers: Int = 1
    var currentPlayer: Int = 1

    var name: String = ""
    var age: Int = 0
    var genderId: Int = 0
    var genderResult: String = ""

    val db = FirebaseFirestore.getInstance()
    var users: MutableList<String> = mutableListOf()

    private lateinit var binding: ActivityPlayerDetailsBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val sharedPref = [email protected](Context.MODE_PRIVATE)
        val applicationID: String? = sharedPref.getString("applicationID", null)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_player_details)

        if (applicationID != null) {

            db.collection("phones").document(applicationID)
                .collection("users")
                .get()
                .addOnSuccessListener { result ->

                    for (document in result){
                        val name = document.get("name").toString()
                        users.add(name)
                    }
                }

            Log.d(FirebaseTAG, users.toString())
            val spinnerAdaptor = ArrayAdapter<String>(this@PlayerDetails, android.R.layout.simple_spinner_item, users)
            spinnerAdaptor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            choose_user?.adapter = spinnerAdaptor
            choose_user.onItemSelectedListener = this@PlayerDetails
        }

        val intent = getIntent()
        numOfPlayers = intent.getIntExtra("number_of_players", 1)
        next_details.setOnClickListener(this)
        player_name.addTextChangedListener(this)
        player_age.addTextChangedListener(this)
        gender.setOnCheckedChangeListener(object: RadioGroup.OnCheckedChangeListener {
            override fun onCheckedChanged(radiogroup: RadioGroup, checked: Int) {
                if (fieldsArePopulated()) next_details.visibility = View.VISIBLE
            }
        })

    }

    override fun onItemSelected(parent: AdapterView<*>?, name: View?, position: Int, rowId: Long) {
        val chosenName: String = parent?.getItemAtPosition(position).toString()
        Log.d("ChosenName", chosenName)
        Log.d("adapterclicked", "adapterclicked")

    }

    override fun onNothingSelected(parent: AdapterView<*>?) {
        Log.d("Nothing", "NOTHINGCALLED")
    }

...

Any idea what the problem is?

Also, when I select an item in the Spinner the view next to my spinner moves, so it's obviously being detected but onItemSelected() is still not being called.

like image 239
Zorgan Avatar asked Mar 21 '19 10:03

Zorgan


1 Answers

I set an initial value to the users MutableList:

var users: MutableList<String> = mutableListOf("Choose User")

and now onItemSelected() is successfully being called.

like image 177
Zorgan Avatar answered Nov 08 '22 18:11

Zorgan