Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding items to a ListView from a Spinner doesn't work
    text
    copied!<p>I have two ArrayLists in my app; one containing a list of all stations (<code>AllStations</code>) and one containing a list of stations that the user has select (<code>MyStations</code>). The user will select the stations from the spinner, which contains all items from the AllStations arraylist, and it will add them the <code>myStations</code> arraylist. There is a list view which displays the data from the <code>myStations</code> arrayList. </p> <p>At the moment when a user selects a station to add to the <code>mystations</code> arraylist, it adds it to "myStations" but the listview does not refresh. I have tried <code>adapter.notifyDataSetChanged()</code> I have spend a couple of day googling etc. and haven't found any reason why it's not refreshing. Can anyone help me?</p> <pre><code>public class MyStations extends Activity{ ListView personalStations; Spinner spinner; ArrayList&lt;String&gt;AllStations = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt;MyStations = new ArrayList&lt;String&gt;(); ArrayAdapter&lt;String&gt; StationNames; ArrayAdapter&lt;String&gt; myStationNames; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.my_stations); assignStations(); spinner = (Spinner) findViewById(R.id.spinner1); StationNames = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, AllStations); spinner.setAdapter(StationNames); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { String selectedStation = spinner.getSelectedItem().toString(); MyStations.add(selectedStation); myStationNames.notifyDataSetChanged(); TextView txt1 = (TextView) findViewById(R.id.textView1); int size = MyStations.size(); String sizes= String.valueOf(size); txt1.setText(sizes); } public void onNothingSelected(AdapterView&lt;?&gt; arg0) { // TODO Auto-generated method stub } }); personalStations = (ListView) findViewById(R.id.myStnList); myStationNames = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, MyStations); personalStations.setAdapter(new ArrayAdapter&lt;String&gt;(MyStations.this, android.R.layout.simple_list_item_1, MyStations)); } public void assignStations(){ ImportData stationData = new ImportData(); stationData.importStations(); AllStations = stationData.getStationNames(); } } </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