6 Replies Latest reply on Jun 28, 2010 5:44 AM by TomBurgundy

    Massive problem with ArrayCollection.getItemIndex()

    TomBurgundy Level 1

      Hi folks


      Flex has been frustrating many times, but this time I have no idea what to do. Please have a look at the following few lines of code, and tell me why the index returned is -1 the second time.


      BTW the Product class has no binding declared, and does not extend or use ObjectProxy.



      this.ac = new ArrayCollection(arr);

      this.ac.sort = new Sort();

      this.ac.sort.fields = [ new SortField("rank", false, true) ];


      var prd:Product = this.ac.getItemAt(2) as Product;

      var index:int = this.ac.getItemIndex(prd); // returns 2

      prd.rank = 3000;

      index = this.ac.getItemIndex(prd); // returns -1



      Also here is the Product class
           public class Product extends Object
                public var title:String;
                public var rank:Number;
                public function Product(title:String=null, rank:Number=NaN)
                     this.title = title;
                     this.rank = rank;


      better formatting

        • 1. Re: Massive problem with ArrayCollection.getItemIndex()
          BhaskerChari Level 4

          Hi TomBurgundy,


          The index returned is -1 the second time because you tried to chnaged the rank property of the product object to another value...which is prd.rank = 3000;


          Actually the getItemIndex(prd) function returns the index value greater than or equla to zero if the product object prd is found a exact match of the object...Exact in the sense it should match all the properties of the Product class then only it will reurn a correct index > -1 other wise it returns -1 which means that no exact match of product object is found.


          So that is the reason why you received index = -1.


          If you have used the same expression without chnaging the prd.rank value you will get the same result as first..


          Hope this is clear...



          Bhasker Chari

          • 2. Re: Massive problem with ArrayCollection.getItemIndex()
            rootsounds Level 4

            You have to refresh the ArrayCollection after changing the ranking. Then it will function as desired.

            • 3. Re: Massive problem with ArrayCollection.getItemIndex()
              Sebastien V. Level 3

              Insert an ArrayCollection.refresh() call after you changed the value of the Product rank.


              The getItemIndex() method is based on the ArrayCollection sort. The collection is supposed to be sorted according to the rank, so if the first element has a rank above 3000, the getItemIndex method will not go further in the collection, assuming that it is well sorted. The refresh method will trigger the sort, and thus allow the getItemIndex method to find the right result.

              • 4. Re: Massive problem with ArrayCollection.getItemIndex()
                Sebastien V. Level 3



                The Product instance is recovered from the ArrayCollection, so there is only one Product instance, used as a local variable (prd) and contained in the ArrayCollection. So the prd.rank = 3000; call will change the value of the object passed to the ArrayCollection.getItemIndex(), but also of the item in the ArrayCollection, since it's the same object.

                • 5. Re: Massive problem with ArrayCollection.getItemIndex()
                  BhaskerChari Level 4

                  Yeah Sebastien you are right......I had thought of the product prd as a seperate object ......I din't see clearly the code that prd is the object from the same arraycollection....in my previous post..


                  Yeah So ArrayCollection.refresh() should resolve the issue ....



                  Bhasker Chari

                  • 6. Re: Massive problem with ArrayCollection.getItemIndex()
                    TomBurgundy Level 1

                    Hi guys


                    thanks a lot for your quick replies. Yeah, calling a refresh or just an itemUpdated(prd) does fix the problem.



                    Now, if you are ready for another challenge, here is my original problem:


                    The ArrayCollection (AC) that contains the product is actually wrapped by a ListCollectionView (LCV), which provides additional filtering. Now, unfortunately this LCV does not update when I call ac.refresh() or ac.itemUpdated(), i.e. the new sort is not reflected when dumping the LCV items. I need to call refresh() or itemUpdated() on the LCV instance itself to make it update.


                    I find it extremely inconvenient having to attach listeners between my collections to provide this kind of behaviour.


                    I know that making the "rank" property bindable solves this problem.


                    Is there another solution?