views:

21

answers:

0

Hi,

Hope you can help me with this. In a Flex 4 app I have a List I use for navigation. When the user clicks on one item the application goes to the appropiate view. Now I want to do some validation before the user moves to the new view, so I wanted to prevent the item selection, make the validation and if it is OK change the selectedItem programmatically.

Initially I can change the selectedItem programmatically but if the event preventDefault is called anytime, I can no longer change the selectedItem programatically.

Here is the code that demostrates the behaviour:

<fx:Script>
    <![CDATA[
        import spark.events.IndexChangeEvent;
        protected function myButton_clickHandler(event:MouseEvent):void{
            if(myList.selectedIndex == 0) myList.selectedIndex = 1;
            else myList.selectedIndex = 0;
        }
        protected function myList_changingHandler(event:IndexChangeEvent):void{
            event.preventDefault();
        }
    ]]>
</fx:Script>
<s:layout>
    <s:VerticalLayout/>
</s:layout>
<s:Button id="myButton" label="Change selected index" click="myButton_clickHandler(event)"/>
<s:List id="myList" changing="myList_changingHandler(event)">
    <s:ArrayCollection>
        <fx:String>Item1</fx:String> 
        <fx:String>Item2</fx:String> 
    </s:ArrayCollection>
</s:List>

1.Click the button. The selectedItem is changed programmatically. 2.Click on the list. The changing handler is called, the preventDefault is executed and the selectedItem is not changed. 2.Click on the button again. The changing handler is executed and the selectedItem does not change.

Many thanks. Rgards!