Flex-尝试将文本文件从列表加载到TextArea



我有一个TextArea和一个DataGrid。我在DataGrid中填充了一个目录中的文本文件列表。现在我正在尝试将所选文件加载到TextArea中。当我从列表中选择一个文件时,我得到了一个空指针异常,这告诉我没有正确访问它。。。我想。

    <s:TextArea id="mainTextField" x="0" y="0" width="730" height="523"
                    editable="true" enabled="true"/>
        <s:DataGrid id="list" x="730" y="0" width="294" height="523" dataProvider="{files}" gridClick="listHandler(event)">
            <s:columns>
                <s:ArrayList>
                    <s:GridColumn dataField="name" headerText="Filename"/>    
                </s:ArrayList>
                </s:columns>
        </s:DataGrid>
...
    private var files:ArrayCollection;
                public var stream:FileStream = new FileStream(); // The FileStream object used for reading and writing the currentFile
                public var currentFile:File;
    private function init():void
                {
                    // Default on start: resolve desktop directory
                    var desktop:File = File.desktopDirectory;
                    // Get desktop directory listing
                    files = new ArrayCollection(desktop.getDirectoryListing());
                }
                protected function listHandler(event:GridEvent):void
                {
                    currentFile = event.target as File;
                    trace(currentFile.nativePath.toString()); **//Error occurrs here**
                    stream = new FileStream();
                    stream.openAsync(currentFile, FileMode.READ);
                    stream.addEventListener(Event.COMPLETE, fileReadHandler);
                    stream.addEventListener(IOErrorEvent.IO_ERROR, readIOErrorHandler);
                }
                public function fileReadHandler(event:Event):void 
                {
                    var str:String = stream.readUTFBytes(stream.bytesAvailable);
                    stream.close();
                    var lineEndPattern:RegExp = new RegExp(File.lineEnding, "g");
                    str = str.replace(lineEndPattern, "n");
                    mainTextField.text = str; 
                    stream.close();
                }

我不能100%确定这一点,但我不认为event.target是GridClick事件中的行(即文件)。我认为GridClick事件更倾向于选择一个单元格。尝试

currentFile = list.selectedItem as File;

如果这不起作用,试试这个:

curentFile = files.getItemAt(event.rowIndex) as File;

相关内容

  • 没有找到相关文章

最新更新