views:

225

answers:

1

hello i'm new to flex builder and trying to populate an array from an external file consisting of a list of strings.

how do i go about that? should i use some sort of a data object?

A: 

Here's an example to get you started:

Sample File (file_with_strings.txt):

one, two, three

Sample App

<?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>

I would just use the HTTPService class to load your external file. You can change the resultFormat to XML, Object, and a few other things if you'd like. Then just customize that updateList() method however.

Hope that helps, Lance

viatropos
The list of strings is actually available. Just need to parse it and add each string to the array. Is there a command that does this or do I need to write the parser and the loop that will do this?
serdar karatekin
do you have a sample string? if you already have a string in the application, you'll have to write a parser. I assumed the strings were comma separated.
viatropos