0

i would be glad if anyone can answer this.

i have an actionscript class as follows:

    package
    {
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;
        import mx.core.UIComponent;

        public class GridComponent extends UIComponent
        {

            private var _array:ArrayCollection;

            public function GridComponent()
            {
            }

            public function get array():ArrayCollection
            {
                return _array;
            }

            public function set array(value:ArrayCollection):void
            {
                _array = value;
                Alert.show(value.length + "");
            }

        }
    }

i invoke this from a mxml file; array variable is a array collection which is pre-populated with some data.

private var array:ArrayCollection = new ArrayCollection();

                            var person1:Person = new Person();
                person1.id = 1;
                person1.name = "pavzie1";

                var person2:Person = new Person();
                person2.id = 2;
                person2.name = "pavzie2";

                var person3:Person = new Person();
                person3.id = 3;
                person3.name = "pavzie3";   

                                array.addItem(person1);
                array.addItem(person2);
                array.addItem(person3);     

    <local:GridComponent array="{array}">

    </local:GridComponent>

Person is a pojo class with 2 instance variables id and name

the result is i get a length of 0. what could be wrong?? Also if i put a number instance variable with setters and getters , it binds the value correctly, so problem is only with binding of an array collection.

5
  • Your problem likely occurs at a higher level of your ArrayCollection data. Can you post code, or confirm your data is valid by using the debugger? Commented Jul 24, 2011 at 21:17
  • What result do you get? Are you referring to the string traced with your trace statement? Or are you expecting some other result? As Jason mentioned, I also suspect the issue is with how you set up your ArrayCollection; or possibly when you set the array property on your custom component. Commented Jul 24, 2011 at 22:16
  • hi, thanks for offering the help so quickly. Commented Jul 25, 2011 at 7:09
  • @Jason , the result was confirmed by putting an alert and also through a debugger trace statement. thanks Commented Jul 25, 2011 at 7:16
  • @flextras, i have edited the question for details of creating a array collection.let me know where i may be wrong. i am just building the pieces for a much more complex project so getting these right is important . thanks Commented Jul 25, 2011 at 7:17

4 Answers 4

1

There is a logical reason this is happening. First, you are calling private var array:ArrayCollection = new ArrayCollection(). This means that this ArrayCollection gets created when your MXML file is instantiated. At this point, you have an empty ArrayCollection. Next, when your component is setup, it has the value of that empty array collection passed to the setter (which at this point is an empty ArrayCollection).

At this point, all is working as planned. However, what isn't working as planned is the binding mechanism. As Michael mentioned, the [Bindable] tag is required on the ArrayCollection:

[Bindable]
private var array:ArrayCollection = new ArrayCollection();

Without this, your component will never be notified of any changes, and it will assume that the value of the empty ArrayCollection hasn't changed since it was passed to the setter.

Sign up to request clarification or add additional context in comments.

Comments

0

Looking at this, I would say that you have failed to bind your arrayCollection, since you are missing the [Bindable]-tag. Try this:

[Bindable]
private var array:ArrayCollection = new ArrayCollection();

var person1:Person = new Person();
...

That should do the trick.

The reason you are getting 0 as a length for your ArrayCollection is probably because you populate your ArrayCollection after your GridComponent was created and at that given point, your ArrayCollection holds no data yet.

Comments

0

I agree with the above answers which observe that the array collection must be bound in order for the code to be updated when the array collection is populated with values. However, the binding expressions in the above answers work best for scalar values not arrays.

The above binding expressions are triggered only if the array collection itself is replaced with another array collection and not if the contents of the same array collection are updated (as appears to be the case here).

In order to detect changes to the contents of the array, it is necessary to add an event listener for the "collectionChange" event like this array.addEventListener("collectionChange", ...);

(It may be more convenient to use the array as the dataProvider with a DataGroup, because the DataGroup already implements the necessary logic.)

Comments

0

Do you mean that it's telling you that the length is 0 while still inside the setter? (The call to Alert.show() is in there.) You'd be better off putting that in a click handler and then clicking on it to debug it.

Depending on what the rest of your code says, the setter is almost definitely being called after the ArrayCollection is initialized, but before anything is put in there. Essentially:

private var array:ArrayCollection = new ArrayCollection();
.
.
.
<GridComponent object>.array = array; // Alert.show() shows "0".
.
.
.
var person1:Person = new Person();
person1.id = 1;
person1.name = "pavzie1";

var person2:Person = new Person();
person2.id = 2;
person2.name = "pavzie2";

var person3:Person = new Person();
person3.id = 3;
person3.name = "pavzie3";   

array.addItem(person1);
array.addItem(person2);
array.addItem(person3);

So you still have three items in that ArrayCollection, but since you're calling that setter before putting anything in there, you're seeing "0", which is correct. Again this depends on what the rest of your code says.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.