0
votes

I coded the following in Flex 4/AS3 and it doesn't worked as expected.

So, I would like to know if there is something wrong with my code... I'll explain it:

TileTest.mxml

<s:Application minWidth="955" minHeight="600" creationComplete="createButtons()"
<fx:Script>
    <![CDATA[
        import Object.TestButton;
        import mx.collections.ArrayList;

        private var _names:Array = ["one", "two", "three"]; 

        public function createButtons():void
        {
            var buttons:ArrayList = new ArrayList();

            for(var a:int = 0; a < _names.length; a++)
            {
                var testButton:TestButton = new TestButton();
                    testButton.customName = _names[a];

                buttons.addItem(testButton);
            }

            myList.dataProvider = buttons;
        }
    ]]>
</fx:Script>
<s:VGroup gap="12" width="100%">
    <s:Label text="Options" fontSize="18" fontWeight="bold" color="#333333" />
    <s:List width="100%" height="100%" id="myList" itemRenderer="Object.TestButton" borderVisible="false">
        <s:layout>
            <s:TileLayout orientation="rows" columnWidth="290" rowHeight="90" columnAlign="left" horizontalGap="0" verticalGap="0" />
        </s:layout>
    </s:List>
</s:VGroup>
</s:Application>

This is my Application. Here, I have a List called myList, where I load some TestButtons. I set a name for each button inside the loop.

TestButton

<s:Group width="300" height="90" click="{ Alert.show(_name); }"
<fx:Script>
    <![CDATA[
        import mx.controls.Alert;

        private var _name:String = "just a test...";

        public function set customName(newName:String):void
        {
            _name = newName;

            Alert.show(_name);

            this.addEventListener(MouseEvent.MOUSE_OVER, function():void{ Alert.show(_name); });
        }
    ]]>
</fx:Script>
<s:BorderContainer accentColor="#000000" width="100%" height="100%" />
</s:Group>

As you can see, I have three Alerts in this component... I did so we can understand the problem.

  • The first Alert occurs when I set the customName, which occurs in the Application, as already shown.

  • The second one should occur on Mouse_Over, as the event listener been added to the Group element.

  • And the third Alert should occur on Click in the Group element.

Now, if I run the resulting swf, I see all the three buttons in the screen and three Alerts, one for each set customName by the Application and it alerts the right name.

If I put the mouse over any button, it doesn't alert any message.

If I click any button, it alerts the default message set to the _name property, which is "just a test..."

I can't understand what is the problem, as I was expecting it to always alert the name set by the Application to each button. Also, I don't want to change the components... what I'm saying is that I would like to have a List and TestButtons with a private String inside.

If the problem is in these specific implementation, so I'll have no other way than change it...

Thank you all!

1

1 Answers

1
votes

The problem is that you have a list of TestButtons in your data provider, instead of just plain data. Those buttons get created, and that is why you see the correct Alert messages.

So, then Flex sees a list with three items and therefore it creates three additional TestButtons to render the data in your list. But those TestButtons are completely new ones, with default values for its properties.

To fix this, it would be better if you had data only in your data provider ArrayList, and you would access it through the data property of your item renderer.