Home » Android » android – I have a spinner I want to take the spinner element save it as a string and pass it to another activity in kotlin-Exceptionshub

android – I have a spinner I want to take the spinner element save it as a string and pass it to another activity in kotlin-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have a spinner

 val spinner = findViewById<Spinner>(R.id.categories_spinner)
                val vehicle = arrayOf(
                    "Автокраны",
                    ...
                )
                val adapter = ArrayAdapter<String>(this, R.layout.custom_spinner, vehicle)

                adapter.setDropDownViewResource(R.layout.custom_spinner)
                spinner.adapter = adapter

                spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                  override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id:Long) {

                  if (position == 0) {
                      visibility()
                      ttx_constraint_0.visibility = View.VISIBLE
                      image_check.setOnClickListener {
                          mkVehicleAutocranes()
                          mViewModel.mkVehicleCompletedEvent.observe([email protected], Observer {
                                 startActivity(Intent([email protected], ListAutocranes::class.java))
                                })
                            }
                        }
                        ...

                     override fun onNothingSelected(parent: AdapterView<*>) {
                    }
                }
            }
        }

On button click I call the function

 private fun mkVehicleAutocranes() {
            mViewModel.mkAutocranes(
                categories_spinner.getItemAtPosition(0).toString()
            )
        }

I think i save value element of spinner and pass it to another activity. But it’s not work.
Please! Somebody help me!

How to&Answers: