10 Replies Latest reply on Feb 28, 2011 4:35 AM by flexillu10

    Checking for an object in an Array Collection

    flexillu10 Level 1

      So i have an Array Collection with one object in, trace output below

       

      (mx.collections::ArrayCollection)#0
        filterFunction = (null)
        length = 1
        list = (mx.collections::ArrayList)#1
          length = 1
          source = (Array)#2
            [0] (Object)#3
              fileName = "belt_002"
              filePath = "/mens/belts/"
          uid = "2EA13990-EEBA-BDD7-8E55-532927494C53"
        sort = (null)
        source = (Array)#2
      

       

      When adding items to the array collection i want to check an object with the same properties doesn't exist.

       

      So how can i check the above object is not already in the array, basically that there is no matching fileName and filePath?

       

      Thanks for your help

        • 1. Re: Checking for an object in an Array Collection
          jfb00 Level 3

          HI,

          Loop thru your arraycollection and check for duplicate data.

          Here is an idea:

          http://www.actionscript.org/forums/showthread.php3?t=177312

          Rgds

           

          Johnny

          • 2. Re: Checking for an object in an Array Collection
            flexillu10 Level 1

            thanks but could  you refer to my object above by any chance?

             

            i don't know what tradeObj.W2G is? Or tradeObj.A??

             

            Anyone?

            • 3. Re: Checking for an object in an Array Collection
              flexillu10 Level 1

              I know this will be fairly simple for experienced flex coders...really frustrating. Here is what i have

               

              var tempObj:Object;
              
                   if (dataProvider.length==0){
                   dataProvider.addItem(event.currentTarget.selectedItem);
                   }
              
               
                   for (var count:int=0; count  < _dataProvider.length; count++){
                        tempObj = _dataProvider.getItemAt(count) as Object;
                                                 
                   if (tempObj.filePath==event.currentTarget.selectedItem.filePath&& tempObj.fileName==event.currentTarget.selectedItem.fileName){
                        Alert.show("already selected");
                        trace("tempObj =" + ObjectUtil.toString(tempObj))
                        trace("currentTarItem = "+ ObjectUtil.toString(event.currentTarget.selectedItem))
                                                      
                   }
                   else if (tempObj.filePath!==event.currentTarget.selectedItem.filePath&& tempObj.fileName!==event.currentTarget.selectedItem.fileName){
                        Alert.show("this can be added");
                           _dataProvider.addItem(event.currentTarget.selectedItem);
                                                      
                                                      
                   }
                   }
              

               

              So why won't this work? It keeps messing up, can someone just give me pseudo code of how to loop through this array collection and add an object only if the object isn't there!

              • 4. Re: Checking for an object in an Array Collection
                miguel8312 Level 3

                buddy post your project up and ill help you. I just cant follow your snippet. i can put a simple sample together for you but it seems like you want help with this specific problem.

                Miguel

                • 5. Re: Checking for an object in an Array Collection
                  flexillu10 Level 1

                  miguel8312 wrote:

                   

                  buddy post your project up and ill help you. I just cant follow your snippet. i can put a simple sample together for you but it seems like you want help with this specific problem.

                  Miguel

                  my project is a bit big to be posting up. A sample would be great.

                   

                  However here is some code snippets. The code doesn't work by the way its just how i've left it for now.

                   

                  this is the change handler for a spark list(s)

                   

                  protected function componentsCheckList_changeHandler(event:IndexChangeEvent):void
                                 {
                                 var tempObj:Object;
                                      
                                 if (_secdataProvider.length==0){
                                      _secdataProvider.addItem(event.currentTarget.selectedItem);
                                      trace("doing the first if");
                                 }
                                      
                                      
                                 for (var count:int=0; count  < _secdataProvider.length; count++){
                                           tempObj = _secdataProvider.getItemAt(count, 0) as Object;
                                                     
                                                     
                                           if (tempObj.filePath==event.currentTarget.selectedItem.filePath && tempObj.fileName==event.currentTarget.selectedItem.fileName){
                                                     Alert.show("already selected");
                                                          
                                                          
                                                          
                                           }
                                           else if (tempObj.filePath!==event.currentTarget.selectedItem.filePath && tempObj.fileName!==event.currentTarget.selectedItem.fileName) {
                                                Alert.show("this can be added");
                                                          
                                                _secdataProvider.addItem(event.currentTarget.selectedItem);
                                                     break;
                                           }
                                 }
                  

                   

                  the spark list is a list of objects.

                   

                  What i want to happen: when i click an item in one of the lists it is added to _secdataprovider array collection if the object doesnt already exist. Simple no?

                  • 6. Re: Checking for an object in an Array Collection
                    jfb00 Level 3

                    Hi,

                    What he ask you is to create a simple example with your array collection data.

                    I have an example that can help you to see how to work with arraycollection.

                    If this is not what you want, please create a working simple example for your case and we can help you out.

                     

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
                         <mx:Script>
                              <![CDATA[
                                   import mx.collections.*;
                                   import mx.controls.Alert;
                                   
                                   [Bindable] public var stateArray:ArrayCollection = new ArrayCollection(
                                        [{label:"AL",data:"Sample5", myList:{path:"/mens/belts/" ,file:"myFile1"}},
                                        {label:"AK", data:"Sample10", myList:{path:"/mens/belts/" ,file:"myFile2"}},
                                        {label:"AR", data:"Sample2", myList:{path:"/mens/belts/" ,file:"myFile3"}},
                                        {label:"AK", data:"Sample1", myList:{path:"/mens/belts/" ,file:"myFile4"}},
                                        {label:"AK", data:"Sample3", myList:{path:"/mens/belts/" ,file:"myFile5"}},
                                        {label:"AL", data:"Sample7", myList:{path:"/mens/belts/" ,file:"myFile6"}},
                                        {label:"AR", data:"Sample9", myList:{path:"/mens/belts/" ,file:"myFile7"}}]);
                    
                                   private function init():void{
                                   }
                                   
                                   private function addItem():void{
                                        var myTemp:String = "myFile4";
                                        var arrayLength:Number = stateArray.length;
                                        
                                        for(var i:int = 0; i < arrayLength; i++)
                                        {
                                             var myObj:Object = stateArray[i].myList as Object;
                                             for (var x:String in myObj){
                                                  if (x == "file"){
                                                       if(myObj[x] == myTemp){
                                                            Alert.show("already selected");
                                                       }else{
                                                            Alert.show("this can be added");     
                                                       }
                                                  }
                                             }
                                        }
                                   }
                                   
                    
                              ]]>
                         </mx:Script>
                         <mx:Button label="Add" click="addItem()" />
                         <mx:DataGrid id="myGrid" dataProvider="{stateArray}" x="0" y="30">
                              <mx:columns>
                                   <mx:DataGridColumn dataField="label"/>
                                   <mx:DataGridColumn dataField="data"/>
                                   <mx:DataGridColumn dataField="myList.path"/>
                                   <mx:DataGridColumn dataField="myList.file"/>
                              </mx:columns> 
                         </mx:DataGrid>
                    
                    </mx:Application>
                    
                    
                    • 7. Re: Checking for an object in an Array Collection
                      flexillu10 Level 1

                      ok well my list is populated by the result of a httpservice that returns XML stucture


                      event.result.images.image

                       

                      each image has an filePath and fileName

                       

                      yours are hard coded so i don't see my data like this but i guess it would look like so. I'm pretty sure they are object proxys?

                       

                      [filePath: /path/to/item, fileName: file_001]
                      [filePath: /path/to/item, fileName: file_002]
                      

                       

                      resulthandler: this is used becuase if one node is returned it breaks.compMenuResultArray is an ArrayCollection

                       

                           protected function HTTPService_resultHandler(event:ResultEvent):void
                                     {
                                          //trace(mx.utils.ObjectUtil.toString(event.result))
                                          
                                          if (event.result.images == null)
                                          {
                                               compMenuResultArray= new ArrayCollection()
                                          }
                                          else if (event.result.images.image is ArrayCollection)
                                          {
                                               compMenuResultArray = event.result.images.image;
                                          }
                                          else if (event.result.images.image is ObjectProxy)
                                          {
                                               compMenuResultArray = new ArrayCollection( [event.result.images.image] );
                                          }
                                     }
                      

                       

                       

                       

                      the objects populate a list its a custom list to force CTRL key down thats all

                       

                      <c:CheckList id="componentsCheckList" change="componentsCheckList_changeHandler(event)" dataProvider="{compMenuResultArray}" itemRenderer="thumbrenderer" width="951" height="323" allowMultipleSelection="true" x="249" y="10">
                                                         <c:layout>
                                                              <s:TileLayout requestedColumnCount="3" horizontalGap="10" verticalGap="10"/>     
                                                         </c:layout>
                      </c:CheckList>
                      

                       

                      changehandler when an item is clicked:

                       

                      protected function componentsCheckList_changeHandler(event:IndexChangeEvent):void
                      {
                                          
                           if (_secdataProvider.length==0){
                                _secdataProvider.addItem(event.currentTarget.selectedItem);
                           }
                                          
                                     
                           var myTemp:String = event.currentTarget.selectedItem.fileName;
                           var arrayLength:Number = _secdataProvider.length;
                                               
                           for(var i:int = 0; i < arrayLength; i++)
                           {
                                var myObj:Object = _secdataProvider[i] as Object;
                                for (var item in myObj){
                                if (item == "fileName"){
                                if(item.fileName == myTemp){
                                     Alert.show("already selected");
                                }else{
                                     Alert.show("this can be added"); 
                                _secdataProvider.addItem(event.currentTarget.selectedItem);
                                }
                           }
                      }
                      
                                
                      
                      

                       

                      trying your code above but no luck. Any help anyone?

                       

                      Thanks for your help so far

                      • 8. Re: Checking for an object in an Array Collection
                        jfb00 Level 3

                        Hi,

                        I don't understand your issue.

                        Are you trying to add item from one array (the one comes from HTTP service) to another array?

                        Place a breakpoint after ADDITEM and see the structure of your data, maybe is just simple structure.

                        Please create a simple working flex 4 example using regular component s:list.

                        Rgds

                         

                        Johnny

                        • 9. Re: Checking for an object in an Array Collection
                          jfb00 Level 3

                          Hi,

                          Try this:

                           

                          protected function componentsCheckList_changeHandler(event:IndexChangeEvent):void
                                      {
                                          if (_secdataProvider.length==0){
                                              _secdataProvider.addItem(event.currentTarget.selectedItem);
                                              _secdataProvider.refresh();
                                          }
                                          
                                          var myTemp:String = event.currentTarget.selectedItem.file;
                                          var arrayLength:Number = _secdataProvider.length;
                                          var flag:Boolean = false;
                                          for(var i:int = 0; i < arrayLength; i++)
                                          {
                                              if(_secdataProvider[i].file == myTemp)
                                                  flag = true;
                                          }
                                          if (flag == true){
                                              Alert.show("already selected");
                                          }else{
                                              Alert.show("this can be added"); 
                                              _secdataProvider.addItem(event.currentTarget.selectedItem);
                                              _secdataProvider.refresh();
                                          }
                                      }
                          

                           

                          Rgds

                           

                          Johnny

                          • 10. Re: Checking for an object in an Array Collection
                            flexillu10 Level 1

                            Brilliant thanks this works great. I got rid of the first if statement though as its not needed.