RapsFan1981
RapsFan1981

Reputation: 1197

Actionscript AIR +show files in list

I'm creating an AIR project in flash and populated a list with files referenced in an external xml. Now 'm trying to populate the list with a directory's contentss, like a file browser. Below is my code

var myDocuments:File = File.documentsDirectory;
var fu:FileUtils = new FileUtils();
var myArray = FileUtils.GetAllFilesFromDir(myDocuments, true);

function PopulateList(event:Event):void {
    for(var i:Number = 1;i< myArray.length;i++) {
        list.addItem( myArray[i] ); // My list box
    }
}

FileUtils is a custom class I came across:

public class FileUtils
    {
        /**
         * Lists all files in a directory structure including subdirectories, except the folders themselves.
         *
         * @param STARTINGFILE File the top level folder to list the contents of
         * @param RELATIVETO File Optional If this is set all paths returned will be relative to this.
         */
        public static function ListAllFiles(STARTINGFILE:File, RELATIVETO:File = null):String
        {
            var str:String = "";

            for each(var lstFile:File in STARTINGFILE.getDirectoryListing())
            {
                if(lstFile.isDirectory)
                {
                    str+= ListAllFiles(lstFile, RELATIVETO);
                }
                else
                {
                    if(RELATIVETO!=null)
                    {
                        str+= RELATIVETO.getRelativePath(lstFile) + "\n";
                    }
                    else
                    {
                        str+= lstFile.nativePath + "\n";
                    }
                }
            }

            return str;
        }

        /**
         * Returns an array populated with File objects representing all the files in the given directory
         * including all the subdirectories but excluding the directory references themselves
         *
         * @param STARTINGFILE File the top level directory to list the contents of
         * @param INCSUB Boolean Optional Include subdirectories
         */
        public static function GetAllFilesFromDir(STARTINGFILE:File, INCSUB:Boolean = true):Array
        {
            var arr:Array = [];

            for each(var lstFile:File in STARTINGFILE.getDirectoryListing())
            {
                if(lstFile.isDirectory && INCSUB)
                {
                    for each(var subFile:File in GetAllFilesFromDir(lstFile, true))
                    {
                        arr.push(subFile);
                    }
                }
                else
                {
                    arr.push(lstFile);
                }
            }
            return arr;
        }
}
}

EDIT:

function PopulateList(event:Event):void {
    for(var i:Number = 1;i< myArray.length;i++) {
        list.addItem( File(myArray[i]).name ); // My list box
    }
}

Upvotes: 1

Views: 1155

Answers (1)

midhunhk
midhunhk

Reputation: 5554

I changed the code in your main class to be like below and its listing me the names of all the files that i have in my documents directory.

  var myDocuments:File = File.documentsDirectory;
var fu:FileUtils = new FileUtils();
var myArray:Array = FileUtils.GetAllFilesFromDir(myDocuments, true);
var arrayColl:ArrayCollection = new ArrayCollection();

for(var i:Number = 1;i< myArray.length;i++) 
{
    var file:File = File( myArray[i]);
    arrayColl.addItem(file.name);
}
list.dataProvider = arrayColl;  

Here the item named list is defined like so in my MXML

<mx:List id="list"/>

addItem() for a list object is used to add a DisplayObject to it. But here we will be setting the dataProvider property to an ArrayCollection of the names of the files.

Oh, and how was your PopulateList function getting invoked?

Upvotes: 1

Related Questions