3 Replies Latest reply on Jan 18, 2007 9:55 AM by FlightGuy

    Find out container items

    weiwuliang
      Hi all,
      I want to find out how many chidren the container have. here is my code:
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute" >
      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;

      private function checkChildren():void{
      var myText:String = new String();
      myText="The HorizontalList container has " + list1.numChildren + " children";
      myText+="\nThe Application has " + numChildren + " children";
      Alert.show(myText);
      }
      ]]>
      </mx:Script>

      <mx:VBox>
      <mx:HorizontalList id="list1" width="300" height="50" dragEnabled="true" borderStyle="solid">
      <mx:dataProvider>
      <mx:Array>
      <mx:Object label="Text1"/>
      <mx:Object label="Text2"/>
      <mx:Object label="Text3"/>
      <mx:Object label="Text4"/>
      </mx:Array>
      </mx:dataProvider>
      </mx:HorizontalList>
      <mx:HorizontalList id="list2" width="300" height="50" dropEnabled="true" borderStyle="solid">

      </mx:HorizontalList>

      <mx:Button label="Check" click="checkChildren()" />
      </mx:VBox>
      </mx:Application>

      in the code I want to find out how many items i have drop into the "list2", but using numChildren always show me the same number?

      Thanks for the help.
        • 1. Find out container items
          QuickStick Level 1
          Even "list1.numChildren" is returning an incorrect value, it says there are 3 children when, you have 4 written in. So I don;t think this is the correct property to return.

          Flex will return an error if you don't include the list2 dataProvider Array. You may want to remove it, and do an error check instead.

          e.g: if (list2.dataProvider.length == null)

          ------------------------

          Changes are highlighted:

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute" >
          <mx:Script>
          <![CDATA[
          import mx.controls.Alert;

          private function checkChildren():void{
          var myText:String = new String();
          myText="The HorizontalList (list1) has " + list1.dataProvider.length + " children";
          myText+="\nThe HorizontalList (list2) has " + list2.dataProvider.length + " children";
          Alert.show(myText);
          }
          ]]>
          </mx:Script>

          <mx:VBox>
          <mx:HorizontalList id="list1" width="300" height="50" dragEnabled="true" borderStyle="solid">
          <mx:dataProvider>
          <mx:Array>
          <mx:Object label="Text1"/>
          <mx:Object label="Text2"/>
          <mx:Object label="Text3"/>
          <mx:Object label="Text4"/>
          </mx:Array>
          </mx:dataProvider>
          </mx:HorizontalList>
          <mx:HorizontalList id="list2" width="300" height="50" dropEnabled="true" borderStyle="solid">
          <mx:dataProvider>
          <mx:Array>
          </mx:Array>
          </mx:dataProvider>

          </mx:HorizontalList>

          <mx:Button label="Check" click="checkChildren()" />
          </mx:VBox>
          </mx:Application>

          -------------------

          Hope that helps.
          • 2. Re: Find out container items
            weiwuliang Level 1
            Thanks for that. But if I use this function for getting the childs of "list2". how can I get the value of each childs?
            • 3. Re: Find out container items
              FlightGuy Level 1
              var data:ArrayCollection = list2.dataProvider;
              for each (var item:String in data)
              trace(item);

              or

              for (var i:int=0; i<data.length; i++)
              trace(data.getItemAt(i));