Actionscript AIR + показать файлы в списке - PullRequest
1 голос
/ 18 мая 2011

Я создаю проект AIR во флэш-памяти и заполняю список файлами, на которые ссылается внешний XML. Теперь я пытаюсь заполнить список содержимым каталога, например, файловым браузером. Ниже мой код

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 - это пользовательский класс, с которым я столкнулся:

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

1 Ответ

1 голос
/ 19 мая 2011

Я изменил код в вашем главном классе, чтобы он был таким, как показано ниже, и в нем перечислены имена всех файлов, которые у меня есть в моем каталоге документов.

  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;  

Здесь элемент с именем list определяется какпоэтому в моем MXML

<mx:List id="list"/>

addItem() для списка используется объект для добавления DisplayObject к нему.Но здесь мы будем устанавливать для свойства dataProvider ArrayCollection имен файлов.

О, а как вызывается ваша функция PopulateList?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...