2 Replies Latest reply on May 12, 2008 8:56 AM by May 2008

    ArrayCollection problem

    MayLam18 Level 1

      Hi everyone,

      I am having problem with assign the ArrayCollection (i.e., arrColl) to a variable (i.e., myDataProvider).
      And LINE XYZ give error when I run the following code. And the following code is for creating paging with TileList which is very simple.

      I need the following for my project. If you have time. Please kindly help me. Thanks.

      May

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init()">
      <mx:Script>
      <![CDATA[
      import mx.collections.XMLListCollection;
      import mx.collections.ArrayCollection;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.events.FaultEvent;
      import mx.controls.Alert;

      [Bindable]private var myDataProvider:ArrayCollection = arrColl;
      private var pagedDataProvider:ArrayCollection;
      [Bindable]private var curPage:int=1;
      [Bindable]private var pageCount:int=0;
      private var PERPAGE:int=2;

      public function init():void{

      pagedDataProvider=new ArrayCollection();
      pageCount=(myDataProvider.length/PERPAGE)+1; // LINE XYZ: Give error due to myDataProvider is null.
      curPage=1;

      if(pageCount > 1){
      btnNext.enabled=true;
      }

      if(myDataProvider.length >= PERPAGE){
      for(var i:int=0;i<PERPAGE;i++){
      pagedDataProvider.addItem(myDataProvider.getItemAt(i));
      }
      }
      else {
      pagedDataProvider=myDataProvider;
      }
      }

      private function getNextPage():void{

      var start:int=PERPAGE*curPage; //for example, click from page 1, start would then be 8*1=8. start at index 8.
      var end:int=0;
      //do we have enough items in this set?
      if((myDataProvider.length-start)>PERPAGE){
      end=start+PERPAGE;//count it
      }else{
      end=myDataProvider.length;
      }
      pagedDataProvider=new ArrayCollection();
      for(var i:int=start;i<end;i++){
      pagedDataProvider.addItem(myDataProvider.getItemAt(i));
      }
      curPage++;//incriment the page!
      btnPrevious.enabled=true;
      if(curPage==pageCount){
      btnNext.enabled=false;
      }
      }

      private function getPreviousPage():void{
      curPage--;//decriment the page!
      btnNext.enabled=true;
      if(curPage==1){
      btnPrevious.enabled=false;
      }
      var start:int=PERPAGE*(curPage-1);//for example, click from page 1, start would then be 8*1=8. start at index 8.
      var end:int=start+PERPAGE;//count it
      pagedDataProvider=new ArrayCollection();
      for(var i:int=start;i<end;i++){
      pagedDataProvider.addItem(myDataProvider.getItemAt(i));
      }
      }

      ]]>
      </mx:Script>

      <mx:ArrayCollection id="arrColl">
      <mx:source>
      <mx:Array>
      <mx:Object source="assets/Accordion.png"
      label="Accordion" />
      <mx:Object source="assets/ApplicationControlBar.png"
      label="ApplicationControlBar" />
      <mx:Object source="assets/Box.png"
      label="Box" />
      <mx:Object source="assets/Button.png"
      label="Button" />
      <mx:Object source="assets/ButtonBar.png"
      label="ButtonBar" />
      <mx:Object source="assets/CheckBox.png"
      label="CheckBox" />
      <mx:Object source="assets/ColorPicker.png"
      label="ColorPicker" />
      <mx:Object source="assets/ComboBox.png"
      label="ComboBox" />
      <mx:Object source="assets/DataGrid.png"
      label="DataGrid" />
      <mx:Object source="assets/DateChooser.png"
      label="DateChooser" />
      <mx:Object source="assets/DateField.png"
      label="DateField" />
      <mx:Object source="assets/HorizontalList.png"
      label="HorizontalList" />
      <mx:Object source="assets/HRule.png"
      label="HRule" />
      </mx:Array>
      </mx:source>
      </mx:ArrayCollection>


      <mx:Button id="btnPrevious" label="Previous" x="43" y="66"/>
      <mx:Button id="btnNext" label="Next" x="155" y="66"/>

      <mx:TileList rowHeight="108" columnWidth="108" left="43" columnCount="4" id="tilelistPagedExample" rowCount="2" dataProvider="{pagedDataProvider}" y="119">
      <mx:itemRenderer>
      <mx:Component>
      <mx:Label width="100" height="100" text="{data.source}" toolTip="{data.label}" />
      </mx:Component>
      </mx:itemRenderer>
      </mx:TileList>


      </mx:Application>
        • 1. Re: ArrayCollection problem
          javamonjoe Level 1
          May,
          If you move your initialization of myDataProvider from the var definition to the init() function your code does not produce the error.

          Something like:
          private function init() : void
          {
          myDataProvider = arrColl;
          ...
          • 2. Re: ArrayCollection problem
            May 2008 Level 1
            quote:

            Originally posted by: javamonjoe
            May,
            If you move your initialization of myDataProvider from the var definition to the init() function your code does not produce the error.

            Something like:
            private function init() : void
            {
            myDataProvider = arrColl;
            ...


            Hi, you are right. Now this code is working. But I had to put the [Bindable] infornt of private var pagedDataProvider:ArrayCollection;

            Thanks again.

            May