Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to display int value obtained from spinner in android
    text
    copied!<p>I am trying to convert a spinner value that i get (based off the position) into an int value which i can then display below the spinner. When i run the code it gets an error when i include the "tv.setText(position);" line but i cant figure out why. I am trying ot set the text of the TextView tv to the position of the spinner. </p> <p>What am i doing wrong?</p> <pre><code>import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class TestThings extends Activity { Spinner spinner1; int yourInt; TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.testing); spinner1 = (Spinner) findViewById(R.id.testspinner1); ArrayAdapter&lt;CharSequence&gt; adapter1 = ArrayAdapter.createFromResource( this, R.array.chpugsp1_array, android.R.layout.simple_spinner_item); adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner1.setAdapter(adapter1); spinner1.setOnItemSelectedListener(new OnItemSelectedListener(){ public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { int position = spinner1.getSelectedItemPosition(); tv = (TextView) findViewById(R.id.testext); tv.setText(position); } @Override public void onNothingSelected(AdapterView&lt;?&gt; arg0) { // TODO Auto-generated method stub } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload