views:

551

answers:

2

Hi,

Can someone lead me to SHIFT an element in an ArrayCollection in flex?

I have an ArrayCollection of sorted objects.

Now i need to move a row to the end of the ArrayCollection.

To illustrate,

arrayCollection = ["Cars","Other","Trucks"];

This ArrayCollection is sorted. Now i need to move 'Other' to the end of the ArrayCollection. i.e., I need the array to be restructured as

arrayCollection  = ["Cars","Trucks","Other"];

Here is my code,

if(Index != -1){ 
CategoryList.addItem(CategoryList.removeItemAt(Index)); 
trace(CategoryList.source.join());}

'CategoryList' is an ArrayCollection of length 28, with 3 attributes for each object in the ArrayCollection.

'RemoveItem' works fine, but'AddItem' throws this error,

RangeError: Index '28' specified is out of bounds. at mx.collections::ArrayList/addItemAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ArrayList.as:305] at mx.collections::ListCollectionView/addItemAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:501] at mx.collections::ListCollectionView/addItem()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:470] at components::Home/creationOver()[C:\Documents and Settings\immanuel\My Documents\Flex Builder 3\Porj\src\components\Home.mxml:113] at components::Home/___Home_Canvas1_creationComplete()[C:\Documents and Settings\immanuel\My Documents\Flex Builder 3\Porj\src\components\Home.mxml:2] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9298] at mx.core::UIComponent/set initialized()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:1169] at mx.managers::LayoutManager/doPhasedInstantiation()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\LayoutManager.as:718] at Function/http://adobe.com/AS3/2006/builtin%3A%3Aapply() at mx.core::UIComponent/callLaterDispatcher2()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8628] at mx.core::UIComponent/callLaterDispatcher()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8568]

I then try to insert at a specific position,

CategoryList.addItemAt(CategoryList.removeItemAt(Index), CategoryList.length-1);

But this, throws the below error,

TypeError: Error #1006: value is not a function. at mx.collections::ListCollectionView/getFilteredItemIndex()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:564] at mx.collections::ListCollectionView/addItemsToView()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:896] at mx.collections::ListCollectionView/listChangeHandler()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:1051] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.collections::ArrayList/internalDispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ArrayList.as:510] at mx.collections::ArrayList/addItemAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ArrayList.as:311] at mx.collections::ListCollectionView/addItemAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:501] at components::Home/creationOver()[C:\Documents and Settings\immanuel\My Documents\Flex Builder 3\Porj\src\components\Home.mxml:113] at components::Home/___Home_Canvas1_creationComplete()[C:\Documents and Settings\immanuel\My Documents\Flex Builder 3\Porj\src\components\Home.mxml:2] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9298] at mx.core::UIComponent/set initialized()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:1169] at mx.managers::LayoutManager/doPhasedInstantiation()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\LayoutManager.as:718] at Function/http://adobe.com/AS3/2006/builtin%3A%3Aapply() at mx.core::UIComponent/callLaterDispatcher2()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8628] at mx.core::UIComponent/callLaterDispatcher()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8568]

A: 

Can you please elaborate with an example ??

Geek
Hi Geek,Thanks for the reply.. I have an array,arr[3] = {"Cars","Other","Trucks"};This array is sorted.Now i need to move 'Other' to the end of the array, i.e i need the array to be restructured as,arr[3] = {"Cars","Trucks","Other"};Hope you understood what i meant.
Immanuel
+1  A: 
var array:Array = ["Cars", "Other", "Trucks"];
pushToEnd(array, 1);

trace(array.join()); //Cars,Trucks,Other

/**
* Removes the item at 'index' and pushes it to the back of the array.
*/

function pushToEnd(array:Array, index:Number):void
{
  array.push(array.splice(index, 1)[0]);
}


It's easier with an ArrayCollection

arrayCol.addItem(arrayCol.removeItemAt(index));

UPDATE: Working sample - see it for yourself.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" 
    creationComplete="create();">
    <mx:Button label="push" click="handle();"/>
    <mx:Script>
     <![CDATA[
      import mx.collections.ArrayCollection;

      private var ac:ArrayCollection;

      private function handle():void
      {
       ac.addItem(ac.removeItemAt(1));
       trace(ac.source.join());
      }

      private function create():void
      {
       ac = new ArrayCollection(["asd", "qwe", "zxc", "123"]);
       trace(ac.source.join());
      }
        ]]>
    </mx:Script>
</mx:Application>
Amarghosh
Removing and adding at the end works.
Amarghosh
Thanks for the reply, and for confirming it works Amarghosh...I am actually using a ArrayCollection and the methods join, push, and splice dont work on an ArrayCollection... I just intended to make my requirement simpler :)
Immanuel
It's easier with array collection - see my update. You will have to reset the sort property to null before doing this though.
Amarghosh
I'd tried this.. But removing and adding an item gives an 'Out of Bounds' exception..
Immanuel
Did you check `if(index < arrayCol.length)` before doing this?
Amarghosh
Yup.. Removing the item isn't a problem.. It gives an exception while adding.. i even tried adding at the 0'th position.. Still gives the same exception... The array length gets reduced on deleting and doesn't increment automatically on 'addItem'?
Immanuel
I don't have flex builder with me now and hence can't check this now. Will check it once I get access to FB.
Amarghosh
Thanks Amarghosh... Appreciate your help..
Immanuel
Tested - it is working perfectly. I've updated my post with a working flex app - run it and keep clicking the button and check the traced output. You are doing something wrong somewhere else. Read the code again and again to see what it is... post the code if you want more help.
Amarghosh
Thanks for the code Amarghosh.. Your sample works just fine.. But in my case, the filter function in ListCollectionView class happens to be null when I try to 'addItem'..
Immanuel
The scope of your question keeps on expanding - why didn't you add all these info in the beginning? Show the code and error message
Amarghosh
Thanks for the help Amarghosh.. I tried to generalize my problem initially to make it simpler.. Please check my edited post... It has my error log in there...
Immanuel