How do I handle click event in Spark List control in Flex 4

12,150

Solution 1

I figured how to do this. Thought I would share so that it helps others like me:

<s:List id="taskList" creationComplete="taskList.addEventListener('listClickEvent',handleListClick);" width="100%" height="80%" labelField="description" dataProvider="{todoList}" 
        useHandCursor="true">
    <s:itemRenderer>
        <fx:Component>
            <s:ItemRenderer click="handleClick(event)">
                <fx:Script>
                    <![CDATA[
                        import ListClickEvent;

                        import flash.events.MouseEvent;

                        import mx.controls.Alert;
                        private function handleClick(me:MouseEvent):void
                        {
                            var listClickEvent:ListClickEvent = new ListClickEvent("listClickEvent");
                            listClickEvent.index = itemIndex;
                            owner.dispatchEvent(listClickEvent);
                        }
                    ]]>
                </fx:Script>
                <s:Label text="{data.description}" top="5" bottom="5" right="3" left="3"/>
            </s:ItemRenderer>
        </fx:Component>
    </s:itemRenderer>
</s:List>   

Solution 2

I know I'm late to the party here, but the simplest way to get the selected node from list in a click event is to use the currentTarget property.

function myClickHandler(event:MouseEvent):void{
   Alert.show("My Var: " + event.currentTarget.selectedItem.myVar);
}

<s:List ... click="myClickHandler(event);">
...
</s:List>

see:

http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7cdb.html

Solution 3

You can use the IndexChangeEvent.CHANGE on List http://docs.huihoo.com/flex/4/spark/events/IndexChangeEvent.html

Package spark.events Class public class IndexChangeEvent Inheritance IndexChangeEvent Event Object

Language Version: ActionScript 3.0 Product Version: Flex 4 Runtime Versions: Flash Player 10, AIR 1.5

The IndexChangeEvent class represents events that are dispatched when an index changes in a Spark component.

See also

spark.components.supportClasses.ListBase spark.components.List spark.components.ButtonBar

Solution 4

Another way:

    <s:List id="myid"
            dataProvider="{listDP}"
            width="100%"
            height="100%"/>

on application creation complete:

myid.addEventListener(MouseEvent.CLICK,clickHandler);

Handler:

  private function clickHandler(event:MouseEvent):void
      {
        if(myid.selectedIndex>=0)
        { 
         ...
        }
          myid.selectedIndex=-1;//to detect click on same item

      }

Solution 5

Thanks guys,

Just make sure your List has its id variable set. Then you call in your click handler function like this:

private function listClickHandler(event:IndexChangeEvent) {
    if(myListsID.seletectedIndex == 0){
        navigator.pushView(whateverViewyouwant)
    } else if(myListsID.selectedIndex ==1){
        navigator.pushView(changetoanotherview)
    } else if(myListsID.selectedIndex == 2){
        navigator.pushView(mobileViewsareEasy)
    } else if(myListsID.selectedIndex == 3){
        navigator.pushView(wowSomanyViews)
    }  

}

The variable that goes into the pushView function corresponds to the mxml file name for the view you want to load

Share:
12,150
motiver
Author by

motiver

Updated on June 12, 2022

Comments

  • motiver
    motiver almost 2 years

    I have a s:List component. I want to handle a click event to know what list item is selected. I don't see Click event in s:List. Any workarounds?

    Thanks.