【问题标题】:flex builder: how to populate an array from an external file of stringsflex builder:如何从外部字符串文件填充数组
【发布时间】:2010-02-27 13:23:38
【问题描述】:

你好,我是 flex builder 的新手,我正在尝试从由字符串列表组成的外部文件填充数组。

我该怎么做呢?我应该使用某种数据对象吗?

【问题讨论】:

    标签: apache-flex arrays builder populate


    【解决方案1】:

    下面是一个帮助您入门的示例:

    示例文件(file_with_strings.txt):

    one, two, three
    

    示例应用

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application
        xmlns:mx="http://www.adobe.com/2006/mxml"
        initialize="initializeHandler()">
    
    
        <mx:Script>
            <![CDATA[
                import mx.collections.ArrayCollection;
    
                protected function initializeHandler():void
                {
                    service.send();
                }
    
                protected function updateList(result:Object):void
                {
                    var array:Array = result.split(/,\s+/);
                    var collection:ArrayCollection = new ArrayCollection(array);
                    list.dataProvider = collection;
                }
    
            ]]>
        </mx:Script>
    
        <mx:HTTPService id="service"
            url="file_with_strings.txt"
            resultFormat="text" result="updateList(event.result)"/>
    
        <mx:List id="list"/>
    
    </mx:Application>
    

    我只会使用HTTPService 类来加载您的外部文件。如果您愿意,可以将resultFormat 更改为 XML、Object 和其他一些内容。然后只需自定义 updateList() 方法即可。

    希望对您有所帮助, 兰斯

    【讨论】:

    • 字符串列表实际上是可用的。只需要解析它并将每个字符串添加到数组中。是否有执行此操作的命令,或者我是否需要编写解析器和执行此操作的循环?
    • 您有示例字符串吗?如果应用程序中已有字符串,则必须编写解析器。我假设字符串是逗号分隔的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多