4 Replies Latest reply on Aug 2, 2011 12:05 AM by zhanglog

    TileList dataProvider problem

    Benoitcn

      I use removeAll function to clean the dataProvider then I additem to it.

      There is an error occured. Because the dataProvider is null !

       

      So I recreate the dataProvider the error still there Why ~~?

       

      this is the code:

       

      var i:int = 0;
         if( TileList.dataProvider )
          TileList.dataProvider.removeAll(); // I remove all items.
         else{  // next time I try to recreate it but I still can't add item to the dataprovider
           // TileList.dataProvider.addItem("Class 1"); I can't do it because the dataprovider is null now
          TileList.dataProvider = new ListCollectionView( );
          while( i++ < 15 ){
           TileList.dataProvider.addItem("Class" + i);
          }
         }
      
        • 1. Re: TileList dataProvider problem
          Pooja Kuber Level 2

          Try to use

           TileList.dataProvider = new ArrayCollection( );

          in else part, instead of

           TileList.dataProvider = new ListCollectionView( );

          Also make a note that the if statement will always return true even if you remove all the elements. That will just remove the data from dataprovider making its length to 0. Instead you can check the if the length of dataprovider is greater than 0.

          if( contentList.dataProvider.length > 0 )

           

          Thanks and Regards,
          .......................................................................................... ..............
          Pooja Kuber | pkuber@infocepts.com | www.infocepts.com

           

          .......................................................................................... ...................

          • 2. Re: TileList dataProvider problem
            Benoitcn Level 1

            error.JPG

            I really confuse Why the dataProvider become null after removed all items ??

            • 3. Re: TileList dataProvider problem
              Pooja Kuber Level 2

              Hmm.. It seems that the data provider is not getting instanciated, which is resulating into null object reference error. I tried out the following code.

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                              layout="absolute">
                  <mx:Script>
                      <![CDATA[
                          import flash.text.engine.ContentElement;
                          import mx.collections.ArrayCollection;

                          public function btnClick():void
                          {
                              var i:int=0;
                              if (contentList.dataProvider.length > 0)
                              {
                                  contentList.dataProvider.removeAll();
                              }
                              else
                              {
                                  contentList.dataProvider=new ArrayCollection();
                                  while (i++ < 15)
                                  {
                                      contentList.dataProvider.addItem("Class" + i);
                                  }

                              }
                          }
                      ]]>
                  </mx:Script>
                  <mx:Canvas width="100%"
                             height="100%">

                      <mx:TileList id="contentList"
                                   visible="true"
                                   width="50%"
                                   height="50%"
                                   horizontalCenter="0"
                                   bottom="10"
                                   horizontalScrollPolicy="off"
                                   verticalScrollPolicy="off"
                                   verticalAlign="middle"
                                   maxColumns="3"
                                   maxRows="5"
                                   rowHeight="{contentList.height*0.20}"
                                   columnWidth="{contentList.width*0.33}"
                                   dataProvider="{new ArrayCollection()}">

                      </mx:TileList>
                      <mx:Button id="btn"
                                 label="Populate Tile List"
                                 width="{btn.label.length * 8}"
                                 height="20"
                                 click="btnClick()">

                      </mx:Button>
                  </mx:Canvas>
              </mx:Application>

              May be this will help you.

               

              Thanks and Regards,
              ...................................................................... ..................................
              Pooja Kuber | pkuber@infocepts.com | www.infocepts.com

               

              ...................................................................... .......................................

              • 4. Re: TileList dataProvider problem
                zhanglog

                before your code

                TileList.dataProvider = new ListCollectionView( );

                you did not set any value to dataProvider,so dataProvider is null,

                and you code

                TileList.dataProvider.removeAll(); // I remove all items.

                never work,because

                   if( TileList.dataProvider )

                is false