I have two ArrayLists in my app; one containing a list of all stations (AllStations
) and one containing a list of stations that the user has select (MyStations
). The user will select the stations from the spinner, which contains all items from the AllStations arraylist, and it will add them the myStations
arraylist. There is a list view which displays the data from the myStations
arrayList.
At the moment when a user selects a station to add to the mystations
arraylist, it adds it to "myStations" but the listview does not refresh. I have tried adapter.notifyDataSetChanged()
I have spend a couple of day googling etc. and haven't found any reason why it's not refreshing. Can anyone help me?
public class MyStations extends Activity{
ListView personalStations;
Spinner spinner;
ArrayList<String>AllStations = new ArrayList<String>();
ArrayList<String>MyStations = new ArrayList<String>();
ArrayAdapter<String> StationNames;
ArrayAdapter<String> 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<String>(this, android.R.layout.simple_list_item_1, AllStations);
spinner.setAdapter(StationNames);
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> 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<?> arg0)
{
// TODO Auto-generated method stub
}
});
personalStations = (ListView) findViewById(R.id.myStnList);
myStationNames = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MyStations);
personalStations.setAdapter(new ArrayAdapter<String>(MyStations.this, android.R.layout.simple_list_item_1, MyStations));
}
public void assignStations(){
ImportData stationData = new ImportData();
stationData.importStations();
AllStations = stationData.getStationNames();
}
}