Home > Enterprise >  unable to get spinner value on android
unable to get spinner value on android

Time:03-22

I am trying to show the spinner value on a textview when I click on it. the current spinner array value is retrieved from firebase. when i click the dropdown, I am able to see the different selection on the spinner. however when i click on any of the selection, my textview did not show the value same goes to the spinner value.

    lateinit var managernames: ArrayList<String>
    editleaveview = findViewById(R.id.EditLeaveTypeTextView)
        managernames= ArrayList()

 editleavespinner = findViewById(R.id.EditSelectLeaveTypeSpinner)
        getManagername()

        val arrayAdapter = ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,managernames)

        editleavespinner.adapter = arrayAdapter
        editleavespinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{

            override fun onNothingSelected(parent: AdapterView<*>?) {
                editleaveview.setTextColor(Color.BLACK)
                Log.d("nothing","test")
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

                editleaveview.setTextColor(Color.BLACK)


                editleaveview.text = editleavespinner.getItemAtPosition(position).toString()
            }
        }

    fun getManagername(){

        managerdatabase  = FirebaseDatabase.getInstance().getReference("Leavetype")
        managerdatabase.addValueEventListener(object :  ValueEventListener  {
            override fun onDataChange(snapshot: DataSnapshot) {

                for (names in snapshot.children){
                    managernames.add(names.child("type").value.toString())
                }
            }
            override fun onCancelled(error: DatabaseError) {
                TODO("Not yet implemented")
            }
        })
    }

CodePudding user response:

Try Adding .notifyDataSetChanged() on the adapter after getting the value.

      fun getManagername() {
                managerdatabase = FirebaseDatabase.getInstance().getReference("Leavetype")
                managerdatabase.addValueEventListener(object : ValueEventListener {
                    override fun onDataChange(snapshot: DataSnapshot) {
        
                        for (names in snapshot.children) {
                            managernames.add(names.child("type").value.toString())
                        }
                       arrayAdapter.notifyDataSetChanged()
                    }
        
                    override fun onCancelled(error: DatabaseError) {
                        TODO("Not yet implemented")
                    }
                })
            }

CodePudding user response:

in your onItemSelected() method just change this

editleaveview.text = editleavespinner.getSelectedItem().toString()
  • Related