title: Kotlin for Android: OnItem Selected Listener
body: <p>I am developing an android project with Kotlin</p>
  
  <p>I would like to get values of x and y outside listener and print the multiplication value on textview gpa1 but they are unchanged </p>
  
  <pre><code>class sem1gpaactivity : AppCompatActivity(){
  var x:Int=0
  var y:Double=0.0
  var z:Double=0.0
  
  override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.sem1gpa)
  val firstterm01 = arrayOf("0", "1", "2", "3", "4", "5", "6", "7", "8")
  
  
  val spinner = findViewById&lt;View&gt;(R.id.spinnerfirsttermh1) as Spinner
  val spinnerArrayAdapter = ArrayAdapter(this, 
  android.R.layout.simple_spinner_item, firstterm01)
  
  spinnerArrayAdapter.setDropDownViewResource
    (android.R.layout.simple_spinner_dropdown_item) // The drop down view
  spinner.adapter = spinnerArrayAdapter
  spinner.spinnerfirsttermh1!!.onItemSelectedListener = object : 
  AdapterView.OnItemSelectedListener {
     override fun onItemSelected(p0: AdapterView&lt;*&gt;?, p1: View?, p2: Int, p3: 
  Long) {
  
        when (p2){
  
            0-&gt;  x=0
            1-&gt;  x=1
            2-&gt;  x=2
            3-&gt;  x=3
            4-&gt;  x=4
            5-&gt;  x=5
            6-&gt;  x=6
            7-&gt;  x=7
            8-&gt;  x=8
  
        }
  
     }
  
      override fun onNothingSelected(p0: AdapterView&lt;*&gt;?) {}
  }
  
  val firstterm02 = arrayOf("A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "F")
  val spinner1 = findViewById&lt;View&gt;(R.id.spinnerfirsttermh11) as Spinner
  val spinnerArrayAdapter1 = ArrayAdapter(this, android.R.layout.simple_spinner_item, firstterm02)
  spinnerArrayAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) // The drop down view
  spinner1.adapter = spinnerArrayAdapter1
  spinner1.spinnerfirsttermh11!!.onItemSelectedListener = object : 
  AdapterView.OnItemSelectedListener {
      override fun onItemSelected(p0: AdapterView&lt;*&gt;?, p1: View?, p2: Int, 
  p3: Long) {
          val spinnerLanguage2 = spinner1.getSelectedItem().toString()
          when (p2){
              0-&gt;  y=4.0
              1-&gt;  y=3.7
              2-&gt;  y=3.3
              3-&gt;  y=3.0
              4-&gt;  y=2.7
              5-&gt;  y=2.3
              6-&gt;  y=2.0
              7-&gt;  y=1.7
              8-&gt;  y=1.3
              9-&gt;  y=1.0
              10-&gt; y=0.0
          }
                            }
      override fun onNothingSelected(p0: AdapterView&lt;*&gt;?) {}
  }
  z=x*y
  gpa1.text = z.toString()
  }}
  </code></pre>
  
  <p>z result zero because x and y are not changed and text view show 000
  i tried to put textiew code in the fun in listener of first spinner and show only x it work but when i put textview code outside listener it show nothing</p>