labelFieldとlabelFunction

前回はListの表示にitemRendererを使いましたが、こんなものを使わなくても実現可能でした。。。

まずはlabelField。

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()">

  <mx:Script>
    <![CDATA[
      import mx.collections.ArrayCollection;
      private var directory:File = File.documentsDirectory;

      private function initApp():void {
          directory.addEventListener(Event.SELECT, directorySelected);
      }
    
      private   function onClick():void {
        try
        {
            directory.browseForDirectory("Select Directory");
        }
        catch (error:Error)
        {
            trace("Failed:", error.message)
        }

      }

      private function directorySelected(event:Event):void {
          directory = event.target as File;
          var files:Array = directory.getDirectoryListing();
          fileList.dataProvider = files;
      }  
      
    ]]>
  </mx:Script>

  <mx:Button label="open" click="onClick()"/>
  <mx:List id="fileList" width="100%" height="100%" labelField="name"/>
</mx:WindowedApplication>

ただ、labelFieldを指定しているだけですね。

<mx:List id="fileList" width="100%" height="100%" labelField="name"/>


次はlabelFunction。

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()">

  <mx:Script>
    <![CDATA[
      import mx.collections.ArrayCollection;
      private var directory:File = File.documentsDirectory;

      private function initApp():void {
          directory.addEventListener(Event.SELECT, directorySelected);
      }
    
      private   function onClick():void {
        try
        {
            directory.browseForDirectory("Select Directory");
        }
        catch (error:Error)
        {
            trace("Failed:", error.message)
        }

      }

      private function directorySelected(event:Event):void {
          directory = event.target as File;
          var files:Array = directory.getDirectoryListing();
          fileList.dataProvider = files;
      }  
      
      private function myLabel(item:Object):String {
        return item.name;
      }
      
    ]]>
  </mx:Script>

  <mx:Button label="open" click="onClick()"/>
  <mx:List id="fileList" width="100%" height="100%" labelFunction="myLabel"/>  
</mx:WindowedApplication>

こっちはlabelFunctionを指定して、次のようなfunctionを使うのがポイントです。
private function XXX(item:Object):String


上のコード例では、labelFieldの場合もlabelFunctionの場合もどちらも同じように表示されます。
スクロールは相変わらず重い気がしますが。。