Note that there are some explanatory texts on larger screens.

plurals
  1. POpopulate spinner from string array
    text
    copied!<p>I'm pretty new to this, and I have a question that is probably pretty simple.</p> <p>I have 3 spinners, and I want to populate each spinner from an array based on the choice the user made on the previous spinners.</p> <p>Right now i have it set up to display a toast with the selected data, but I want to set up an activity to open up, but that's later. Right now I need to know how to populate the spinner from an array from the strings.xml.</p> <p>.Java</p> <pre><code>import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; /** * @author madonk * */ public class Region extends Activity { Spinner sp1,sp2,sp3; ArrayAdapter&lt;String&gt; reg_adp,sw_city_adp,sw_lake_charles_adp; List&lt;String&gt; regions,sw_cities,sw_lake_charles; int pos; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_region); regions=new ArrayList&lt;String&gt;(); regions.add("Select a Region"); regions.add("Southwest"); sp1= (Spinner) findViewById(R.id.regions_spinner); sp2= (Spinner) findViewById(R.id.sw_city_spinner); sp3= (Spinner) findViewById(R.id.sw_lake_charles_spinner); reg_adp=new ArrayAdapter&lt;String&gt; (this,android.R.layout.simple_dropdown_item_1line,regions); reg_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); sp1.setAdapter(reg_adp); sp1.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub pos=arg2; add(); } private void add() { // TODO Auto-generated method stub Toast.makeText(getBaseContext(), ""+pos, Toast.LENGTH_SHORT).show(); switch(pos) { case 0: sw_cities= new ArrayList&lt;String&gt;(); sw_cities.add("Select a City"); sw_city_adp=new ArrayAdapter&lt;String&gt;(Region.this, android.R.layout.simple_dropdown_item_1line,sw_cities); sw_city_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); sp2.setAdapter(sw_city_adp); select(); break; case 1: sw_cities= new ArrayList&lt;String&gt;(); sw_cities.add("Select a City"); sw_cities.add("Lake Charles"); sw_cities.add("Iowa"); sw_cities.add("Lake Arthur"); sw_city_adp=new ArrayAdapter&lt;String&gt;(Region.this, android.R.layout.simple_dropdown_item_1line,sw_cities); sw_city_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); sp2.setAdapter(sw_city_adp); select(); break; } } private void select() { // TODO Auto-generated method stub sp2.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelectedregions(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } public void onNothingSelected(AdapterView&lt;?&gt; arg0) { // TODO Auto-generated method stub } public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } public void onNothingSelectedregions(AdapterView&lt;?&gt; arg0) { // TODO Auto-generated method stub } }); } public void onNothingSelectedcities(AdapterView&lt;?&gt; arg0) { // TODO Auto-generated method stub } public void onNothingSelected(AdapterView&lt;?&gt; arg0) { // TODO Auto-generated method stub } }); } </code></pre> <p>stings.xml</p> <pre><code>&lt;resources&gt; &lt;string name="app_name"&gt;Louisiana Festivals&lt;/string&gt; &lt;string name="menu_settings"&gt;Settings&lt;/string&gt; &lt;string name="title_activity_region"&gt;Select a Region&lt;/string&gt; &lt;string-array name="regions_array"&gt; &lt;item &gt;Southwest&lt;/item&gt; &lt;/string-array&gt; &lt;string name="select_a_region"&gt;Select a Region&lt;/string&gt; &lt;string-array name="southwest_cities"&gt; &lt;item &gt;Lake Charles&lt;/item&gt; &lt;item &gt;Iowa&lt;/item&gt; &lt;/string-array&gt; &lt;string name="sw_select_a_city"&gt;Select a City&lt;/string&gt; &lt;string-array name="lake_charles"&gt; &lt;item&gt;Contraband days Pirate Festival&lt;/item&gt; &lt;item&gt;Other festival to be determined&lt;/item&gt; &lt;/string-array&gt; &lt;/resources&gt; </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