简体   繁体   English

通过Flex中的ActionScript循环创建MXML组件

[英]Creating MXML components via an ActionScript loop in Flex

I'm using Flash Builder 4.6 to create an app for a uni project. 我正在使用Flash Builder 4.6为uni项目创建应用程序。 I have a custom Spark component, and I need to dynamically create a number of instances of that component at runtime depending on the number of XML elements returned via a PHP script. 我有一个自定义Spark组件,我需要在运行时动态创建该组件的许多实例,具体取决于通过PHP脚本返回的XML元素的数量。

That might be a bit confusing, so let me write the steps: 1) The application sends an HTTPService request to a PHP script hosted on the server. 这可能有点令人困惑,所以让我编写以下步骤:1)应用程序将HTTPService请求发送到服务器上托管的PHP脚本。 2) The PHP accesses the SQL database and returns a series of XML data. 2)PHP访问SQL数据库并返回一系列XML数据。 3) The ActionScript dynamically creates X instances of my custom Flex component, where X is the number of data in the XML. 3)ActionScript动态创建我的自定义Flex组件的X实例,其中X是XML中的数据数。

Here's the code I've got so far (untidy because I'm trying to make it work): 这是我到目前为止的代码(不整洁,因为我试图让它工作):

ActionScript: 动作脚本:

        [Bindable]
        public var holderArray:Array = new Array(100);

        public function createMenu(e:MouseEvent):void {
            var count:int = 0;
            var curMenuItem:menuItemContainer = new menuItemContainer();
            while (count < loadedMenu.length){
                curMenuItem.itemName = loadedMenu.getItemAt(count).name;
                curMenuItem.itemDesc = loadedMenu.getItemAt(count).description;
                curMenuItem.itemPrice = numForm.format(loadedMenu.getItemAt(count).price);                  
                curMenuItem.imageFile = loadedMenu.getItemAt(count).url;
                //curMenuItem.y = count * 120
                //menuItemGroup.addElement(curMenuItem);
                holderArray[count] = curMenuItem;
                count ++;
            }
            //testString = holderArray[1].itemName;
            var count2:int = 0;
            for each (var menuItem:menuItemContainer in holderArray){
                menuItem.name = "menuItem" + count2;
                menuItem.id = "menuItem" + count2;
                //testString += menuItem.name;
                menuItemGroup.addElement(menuItem);
                count2++;
            }
        }

MXML: MXML:

<s:VGroup id="menuItemGroup" x="40" y="150">
</s:VGroup>

What seems to be happening with that code is that each of my three XML data that get returned are being used in instances of menuItemContainer, but when each one is added to menuItemGroup, it's overwriting the one that's already there. 该代码似乎正在发生的事情是我返回的三个XML数据中的每一个都在menuItemContainer的实例中使用,但是当每个数据被添加到menuItemGroup时,它将覆盖已经存在的那个。 I'm not sure if the item is actually getting overwritten, or if the new item is just sitting atop the earlier ones, but if the latter is true I can't find a way to arrange the components. 我不确定该项目是否实际被覆盖,或者新项目是否只是位于较早的项目之上,但如果后者为真,我找不到安排组件的方法。 I've tried setting menuItem.y in the loop (as a function of count2), but to no avail. 我已经尝试在循环中设置menuItem.y(作为count2的函数),但无济于事。

Thanks in advance for any and all suggestions/answers. 提前感谢任何和所有建议/答案。 Benjamin. 本杰明。

Put this line 把这一行

var curMenuItem:menuItemContainer = new menuItemContainer();

inside your while loop. 在你的while循环中。 With your code as it is you are only creating one instance of menuItemContainer then continually changing the properties of that one menuItemContainer in your while loop. 随着你的代码,因为它是你只创建一个实例menuItemContainer然后不断变化的那一个属性menuItemContainer在while循环。 Instead you need to create a new, different instance of menuItemContainer with each iteration of the loop. 相反,您需要在循环的每次迭代中创建一个新的不同的menuItemContainer实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM