Whistler Whistler - 3 months ago 16
Android Question

Spinner replacing array adds new entries instead

I have a spinner that I want to change the available items based on the program state. I use the arrayadapter class like below

ArrayAdapter array;
array = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, Movements);
array.NotifyDataSetChanged();
spnMove.Adapter = array;


but if I run through this code second time, It just adds the new lisr to the bottom instead of replacing it.

I've already tried setting the .Adapter reference to NULL before adding the new array, but this made precisely no difference at all.

Answer

Instead of creating a new ArrayAdapter for a new dataset, try just updating the data. First call the clear() method on the adapter to delete all old data. Then call add() or addAll() on the ArrayAdapter to add the new data. Finally call notifyDataSetChanged() to make sure the list is notified.

Comments