4 Replies Latest reply on May 10, 2011 10:00 AM by Flex harUI

    possible bug : ListCollectionView.addAll with a filter function

    m1kal_ Level 2

      Hi,

       

      I've wrote this post in the "General Discussion" of the "LFex sdk" forum, I'm also putting it there.

       

      I think I found a bug in the flex SDK. I've checked in JIRA and it seems that it is not yet reported.
      This simple code raise a "out of bouds exception" (flex SDK 4.1) :

       

       

      var list:ArrayCollection = new ArrayCollection();

      list.filterFunction = filter;

      list.refresh();

       

      var items:ArrayList = new ArrayList([1,2,3,4,5]);

      list.addAll(items);

       

      function filter(obj:Object) : Boolean {

           var n:Number = obj as Number;

           return n<2;

      }

       

       

      I've checked the code in mx.collections.ListCollectionView and it's obvious : addAll is calling addAllAt. "addAllAt" is calling by looping over the list to add and calling addItemAt for each item, but it increments the index arguments even if the previous item was not really added (due to a filter restriction), and the "out of bounds exception occurs". In my opinion, addAllAt has to be changed as folowing :

       

      current version :

       

      public function addAllAt(addList:IList, index:int):void {

           var length:int = addList.length;

           for (var i:int=0; i < length; i++)

           {

                this.addItemAt(addList.getItemAt(i), i+index);

           }

      }

       

       

       

      proposed version :

       

      public function addAllAt(addList:IList, index:int):void {

           var length:int = addList.length;

           for (var i:int=0; i < length; i++)

           {

                var at:int = i+index;

                if(at>length) {

                     at = length;

                }

                this.addItemAt(addList.getItemAt(i), at);

           }

      }

       

      Should I open a bug? Or am I missing something?

       

      M1kal