Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I show options in a spinner that, when selected, save that option as an integer variable?
    primarykey
    data
    text
    <p>I'm working on an app that helps a rental business. I have a spinner that pulls in it's content from an array...</p> <p>From datalist.xml :</p> <pre><code>&lt;string-array name="dur"&gt; &lt;item&gt;@string/dur1&lt;/item&gt; &lt;item&gt;@string/dur2&lt;/item&gt; &lt;item&gt;@string/dur3&lt;/item&gt; &lt;item&gt;@string/dur4&lt;/item&gt; &lt;item&gt;@string/dur5&lt;/item&gt; &lt;item&gt;@string/dur6&lt;/item&gt; &lt;item&gt;@string/dur7&lt;/item&gt; &lt;item&gt;@string/dur8&lt;/item&gt; &lt;item&gt;@string/dur9&lt;/item&gt; &lt;item&gt;@string/dur10&lt;/item&gt; &lt;item&gt;@string/dur11&lt;/item&gt; &lt;/string-array&gt; </code></pre> <p>strings.xml :</p> <pre><code>&lt;string name="dur1"&gt;Half hour&lt;/string&gt; &lt;string name="dur2"&gt;1 hour&lt;/string&gt; &lt;string name="dur3"&gt;2 hours&lt;/string&gt; &lt;string name="dur4"&gt;3 hours&lt;/string&gt; &lt;string name="dur5"&gt;4 hours&lt;/string&gt; &lt;string name="dur6"&gt;5 hours&lt;/string&gt; &lt;string name="dur7"&gt;6 hours&lt;/string&gt; &lt;string name="dur8"&gt;Over night&lt;/string&gt; &lt;string name="dur9"&gt;24 hours&lt;/string&gt; &lt;string name="dur10"&gt;Week&lt;/string&gt; &lt;string name="dur11"&gt;Month&lt;/string&gt; </code></pre> <p>Spinner code in java :</p> <pre><code>Spinner spinner = (Spinner) findViewById(R.id.spinnerDuration); ArrayAdapter &lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(this, R.array.dur, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener( new OnItemSelectedListener() { public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { int index = arg0.getSelectedItemPosition(); // storing string resources into Array dur = getResources().getStringArray(R.array.dur); } public void onNothingSelected(AdapterView&lt;?&gt; arg0) { // do nothing } } ); </code></pre> <p>How can I set it up so when the user selects an option from the spinner it then saves that option as an integer variable, reflecting the hours, which can then be used in other parts of this application?</p> <p>For example...Someone selects "3 hours" and it saves a variable called 'durnumb' as 3. Or someone selects "Week" and it saves the value for durnumb as 168 (24hrs x 7days)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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