skip to Main Content

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, 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")
            }
        })
    }

2

Answers


  1. 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")
                        }
                    })
                }
    
    Login or Signup to reply.
  2. in your onItemSelected() method just change this

    editleaveview.text = editleavespinner.getSelectedItem().toString()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search