Flex ArrayCollection-访问对象方法/属性



也许我没有理解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对象的数组。对于数据绑定,我将其放入一个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) [...]
<?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 中

相关内容

  • 没有找到相关文章

最新更新