I am using a spark List and wanted to maintain the selection after changing the content of the list that is the data provider. If you set a new data provider the List sets its selectedIndex back to -1. We solved this problem by intercepting the valueCommit event that is fired when teh List wants to set itself back to -1 and setting the previously selected item (if the new data provider still contains it). This works so far but we are getting strange behaviour:
- initially the previously selected item is still selected and highlighted as intended
- if another item is selected, the highlighting stays on the intial item. It doesn't matter how often I select another one, the initial item is still highlighted but not selected. The newly selected item is in fact selected and also highlighted.
- if the initial item is selected again the List is behaving normal again. When I select another item after I reselected the initial one once, the highlighting disapears.
The List is declared in an MXML like this:
<s:List dataProvider="{model.dataProvider}"
selectedIndex="@{model.selectedIndex}"
valueCommit="model.handleInputObjectListValueCommit(event)"/>
The code in the model class is very complex but this should be the relevant part:
[Bindable]
public var dataProvider:ArrayCollection;
[Bindable]
public var selectedIndex:int;
private var _indexToSelect:int = -1;
public function setNewContent(newContent:ArrayCollection):void {
undoManager.ignore(function ():void {
dataProvider.removeAll();
dataProvider.addAll(newContent);
_indexToSelect = selectedIndex;
});
}
public function handleValueCommit(event:Event):void {
if (_indexToSelect != -1) {
const localIndex:int = _indexToSelect;
_indexToSelect = -1;
selectedIndex = localIndex;
}
}
The undManager is a class that takes care of undo/redo. The ignore function takes care that the undoManager doesn't register the change in the dataProvider as an undoable action because only user interaction should be undoable.
any ideas?