4 Replies Latest reply on Aug 30, 2011 7:36 AM by miguel8312

    Singleton Class ArrayCollection within Flashbuilder (Mobile) empty?

    anixan

      Hi,

       

      I am currently trying to implememnt a Singleton Class in order to store a ArrayCollection of items that I can then access and manipulate across the lifecycle of my app. I have created the below Singleton Class that is designed to hold the ArrayCollection information:

       

      package valueObjects
      {
          import mx.collections.ArrayCollection;    
          
          [Bindable]
          public class Model
          {
              private static var instance:Model = new Model();
              
              public var ids:ArrayCollection = new ArrayCollection();
                                      
              public function Model()
              {
                  if(instance)
                  {
                      trace("New instance cannot be created. Use Singleton.getInstance()");
                  }
              }
              
              public static function getInstance():Model
              {
                  return instance;
              }
          }
      }

       

      I have then created the following code on the Main Deafult page for my application so that the ArrayCollection is populated as soon as the app is initiated:

       

                  import valueObjects.Model;            
                  
                  protected var models:Model = new Model();
      
                              private function loop():void
                  {
                      var index:int;
                      for( index = 0; index < compsCollection.length; index++ )
                      {
                          trace( "Element " + index + " is " +  compsCollection[index].comp_id );
                                      models.ids.addItem(compsCollection[index].comp_id);
                          trace(models.ids.length);
                      }                
                  }
      

       

      The ArrayCollection in the Singleton Class is being populated as the trace statement that I have entered into the loop clearly shows the build of of data in the ArrayCollection. However then when I move to another view within the application I then try to access this ArrayCollection within the Singleton Class with the following code:

      import valueObjects.Model;
      
      protected var models:Model = Model.getInstance();
      protected var test:ArrayCollection = new ArrayCollection();
      
      protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
                  {
                      
                      var index:int;
                      trace("Array Length =" + models.ids.length);
                      for( index = 0; index < models.ids.length; index++ )
                      {
                          trace( "Element " + index + " is " + models.ids[index].comp_id );
                          test.addItem(models.ids[index].comp_id);
                      }    
                      testbox.text = test.toString();
                  }
      

       

      Now the problem I am having is that when I try to access this ArrayCollection(ids) it appears to be empty for some reason. I have included a trace statement that also says that the length of the ArrayCollection is "0". Can anyone please help??

        • 1. Re: Singleton Class ArrayCollection within Flashbuilder (Mobile) empty?
          miguel8312 Level 3

          if your value object class change this

          change this

          public var ids:ArrayCollection = new ArrayCollection();
          to

          public static var ids:ArrayCollection = new ArrayCollection();
          

           

          on your default page change this

          models.ids.addItem(compsCollection[index].comp_id);

          to

          valueObjects.Model.ids.addItem(compsCollection[index].comp_id);
          

           

          on your view change to something like this.

          protected
          
           function view1_viewActivateHandler():void
                    {
                         //initialize="view1_viewActivateHandler();"
                         var index:int;
                         trace("Array Length =" + valueObjects.Model.ids.length);
                         for( index = 0; index < valueObjects.Model.ids.length; index++ )
                         {
                              trace( "Element " + index + " is " + valueObjects.Model.ids[index].comp_id );
                              test.addItem(valueObjects.Model.ids[index].comp_id);
                         }    
                         //testbox.text = test.toString();
                    }
          
          
          
          


          good luck let me know if you have any question i tested this and it worked like a charm for me.

          Miguel

          • 2. Re: Singleton Class ArrayCollection within Flashbuilder (Mobile) empty?
            anixan Level 1

            Thanks a lot for the help and i feel i am a lot closer to having this resolved. The code now seems to work for the most part and when i run in debug mode and open up the view i can see that the ArrayCollection is populated corrected from the Trace Statement:

             

            trace("Array Length = " + valueObjects.Model.ids.length);

             

            However when it goes to the run the code after this:

             

                            for( index = 0; index < valueObjects.Model.ids.length; index++ )
                            {
                                trace( "Element " + index + " is " + valueObjects.Model.ids[index].comp_id );
                                test.addItem(valueObjects.Model.ids[index].comp_id);
                            }
            
            

            I seem to get this error:

            Error #1069: Property comp_id not found on Number and there is no default value.
            

             

            Any ideas why? Thanks

            • 3. Re: Singleton Class ArrayCollection within Flashbuilder (Mobile) empty?
              anixan Level 1

              Ok i have solved that issue and now all works fine. I was mistakingly tring to access a column reference in the ArrayCollection that didnt exist. Now works like a chram thanks again for all the help!

              • 4. Re: Singleton Class ArrayCollection within Flashbuilder (Mobile) empty?
                miguel8312 Level 3

                just checking back in glad you figuered it out the last little piece i recieved the same error when i was testing but i figuered i just did not  have all the code and you had that property in your class.

                Good luck with your app.

                Miguel

                1 person found this helpful