0

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();
    }

}
4

1 回答 1

1

你写:

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));

从我看到的情况来看,您创建了一个适配器并将其分配给myStationsNamesListView您设置了一个新的ArrayAdapter. notifyDataSetChanged()在这种情况下,调用myStationsNames不会做任何事情,因为它没有绑定到小部件。你确定你不想写:

personalStations = (ListView) findViewById(R.id.myStnList);
myStationNames = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MyStations);
personalStations.setAdapter(myStationsNames);
于 2013-03-24T11:50:41.753 回答