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...
You have to refresh the ArrayCollection after changing the ranking. Then it will function as desired.
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.
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.
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 ....
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?