2011-06-18 205 views
1

也許我沒有得到一個ArrayCollection的實際意義,但首先一些代碼Flex ArrayCollection - 訪問對象方法/屬性?

public var test1:AkwRep = new AkwRep(1,200,200,2,86,2010,2012,334342,"Typ","Standort","Testname","url","owner",true); 

// Objekte in ein Array 
public var akwArray:Array = new Array(15); 

public function addAkw():void { 
    akwArray[0] = test1; 
} 

public var akwList:ArrayCollection = new ArrayCollection(akwArray); 

(AKW意味着Atomkraftwerk - >核電廠;))

所以我用akwRep-Objects獲得了一個數組。對於數據綁定,我把它放到ArrayCollection中。 到目前爲止沒有問題。但現在我想要做的事就像

<s:Label text={akwList.getItemAt(0).getAkwName()} /> 

而getAkwName是AkwRep.as的方法,它返回一個字符串。但這不起作用 - 我無法通過ArrayCollection訪問任何方法或屬性。

有沒有解決方案?如果我與陣列嘗試,FlexBuilder中說,他不能這樣做綁定與akwArray [0] ...

編輯:一些新的代碼

這是<fx:script>標籤在我的主要的應用程序

[Bindable] 
// AKW-Objekte erstellen 
public var test1:AkwRep = new AkwRep(1,200,200,2,86,2010,2012,334342,"Typ","Standort","Testname","url","owner",true); 

[Bindable] 
// Objekte in ein Array 
public var akwArray:Array = new Array(15); 

public function addAkw():void { 
    akwArray[0] = test1; 
} 

[Bindable] 
public var akwList:ArrayCollection = new ArrayCollection(akwArray); 

public function init():void{ 
    trace(akwList.getItemAt(0)); 
} 

,這是我AkwRep.as

public class AkwRep 
{ 
    // Attribute 
    // some more attributes right here 

    public var typ:String; 
    public var standort:String; 
    private var akwName:String; 

    [Bindable] 
    public function get AkwName():String { 
     return this.akwName; 
    } 

    // Konstruktoren 
    public function AkwRep(id:Number, x:Number, y:Number, alter:Number, amNetz:Number, offOhneVerl:Number, offMitVerl:Number, leistung:Number, typ:String, standort:String, akwName:String, wikiurl:String, owner:String, moratorium:Boolean) [...] 

回答

0
<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       creationComplete="init()" xmlns:mx="library://ns.adobe.com/flex/mx"> 
    <fx:Script> 
     <![CDATA[ 
      import mx.collections.ArrayCollection; 

      [Bindable] 
      public var akwList:ArrayCollection = new ArrayCollection(); 
      public var test1:AkwRep = new AkwRep(1,"Testname"); 

      public function addAkw():void { 
       akwList.addItem(test1); 
      } 

     ]]> 
    </fx:Script> 

    <mx:VBox> 
     <s:Label text="{AkwRep(akwList.getItemAt(0)).akwName}" /> 
     <s:Button click="addAkw();" label="Add" /> 
    </mx:VBox> 
</s:Application> 

在MXML

package { 

    [Bindable] 
    public class AkwRep { 

     private var _akwName:String; 

     public function get akwName():String { 
      return "Name: " + _akwName; 
     } 

     public function AkwRep(id:Number, akwName:String) { 
      _akwName = akwName; 
     } 
    } 
} 

在AkwRep.as

+0

首先的tahnk你:)我的整個AkwRep.as是綁定的。是否有「_」的理由? – ABLX

+0

「_」通常用於使用get/set函數的變量。這只是一個標準。如果你看看Adobe發佈的flex sdk庫。幾乎每個班級都會看到它的出現;) –

+1

您的akwList聲明上方有[Bindable]標籤嗎? –