1

Maybe i didn't get the real meaning of the ArrayCollection, but first of all some code

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 means Atomkraftwerk -> nuclear power plant ;) )

so i've got an array with akwRep-Objects. For databinding i put it into a ArrayCollection. No problems so far. But now i want to do something like

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

while getAkwName is a method in AkwRep.as which returns a string. but this didn't work - I can not acces any methods or attributes via ArrayCollection.

Is there a solution? If i try it with the array, flexbuilder says he can't do databinding with akwArray[0] ...

Edit: some new code

This is in the <fx:script> tag in my main app

[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));
}

and this is my 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) [...]
ildjarn
  • 62,044
  • 9
  • 127
  • 211
ABLX
  • 716
  • 2
  • 7
  • 18

1 Answers1

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>

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

In AkwRep.as

Sean Thayne
  • 863
  • 1
  • 7
  • 9
  • first of all tahnk you :) my whole AkwRep.as is bindable. is there a reason for "_"? – ABLX Jun 18 '11 at 00:39
  • "_" is usually used for variables that use get/set functions. It's just a standard thou. If you look at flex sdk library that adobe sends out. You'll see occurrences of it in almost every class ;) – Sean Thayne Jun 18 '11 at 00:45
  • 1
    You have the [Bindable] tag above your akwList declaration? – Sean Thayne Jun 18 '11 at 00:46
  • Skip the Array! ;) Just use the ArrayCollection and it should work. I added code at the end of my answer to show what I mean. – Sean Thayne Jun 18 '11 at 01:02
  • Basically your adding to an array, but that array isn't binded to anything. When you create the ArrayCollection it copies the values it doesn't copy the Array. So any new values added to the Array after you create the ArrayCollection won't be added to the Collection ;) – Sean Thayne Jun 18 '11 at 01:03
  • i'm really glad you answer me :) but this also didn't work :/ made it exactly like your code. by the way, when i wirte [Bindable] in AkwRep.as for the getter, flexbuilder says this is not neccesary and will be ignored?! – ABLX Jun 18 '11 at 01:10
  • I modified my answer. I tested out my solution. Seems to work. I'm not totally sure where you went wrong. But maybe you can see from the code above what your missing. – Sean Thayne Jun 18 '11 at 01:30
  • in germany we would say "schwere geburt". thank you! but one last question: ist {AkwRep(...} something like a cast? – ABLX Jun 18 '11 at 01:52
  • Yup, it's type casting. Not required, but it will throw a warning in the errors screen without it. Plus it's nice to have it there for autocomplete functionality and where you typechecking. – Sean Thayne Jun 18 '11 at 02:55