移动ArrayCollection中的元素 - FLEX

时间:2009-11-16 06:52:49

标签: flex actionscript-3

有人可以引导我在flex中的一个ArrayCollection中移动一个元素吗?

我有一个排序对象的ArrayCollection。

现在我需要将一行移到ArrayCollection的末尾。

为了说明,

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

此ArrayCollection已排序。现在我需要将'Other'移动到ArrayCollection的末尾。即,我需要将数组重组为

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

这是我的代码,

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

'CategoryList'是一个长度为28的ArrayCollection,ArrayCollection中的每个对象都有3个属性。

'RemoveItem'工作正常,但'AddItem'会抛出此错误,

  

RangeError:指定的索引'28'超出范围。       在mx.collections :: ArrayList / addItemAt()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ collections \ ArrayList.as:305]       在mx.collections :: ListCollectionView / addItemAt()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ collections \ ListCollectionView.as:501]       在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]       在flash.events::EventDispatcher/dispatchEventFunction()       at flash.events::EventDispatcher/dispatchEvent()       在mx.core :: UIComponent / dispatchEvent()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ core \ UIComponent.as:9298]       在mx.core :: UIComponent / set initialized()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ core \ UIComponent.as:1169]       在mx.managers :: LayoutManager / doPhasedInstantiation()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ managers \ LayoutManager.as:718]       在功能/ http://adobe.com/AS3/2006/builtin::apply()       在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]

然后我尝试插入特定位置,

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

但是,这会抛出以下错误,

  

TypeError:错误#1006:value不是函数。       at mx.collections :: ListCollectionView / getFilteredItemIndex()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ collections \ ListCollectionView.as:564]       在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]       在flash.events::EventDispatcher/dispatchEventFunction()       at flash.events::EventDispatcher/dispatchEvent()       在mx.collections :: ArrayList / internalDispatchEvent()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ collections \ ArrayList.as:510]       在mx.collections :: ArrayList / addItemAt()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ collections \ ArrayList.as:311]       在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]       在flash.events::EventDispatcher/dispatchEventFunction()       at flash.events::EventDispatcher/dispatchEvent()       在mx.core :: UIComponent / dispatchEvent()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ core \ UIComponent.as:9298]       在mx.core :: UIComponent / set initialized()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ core \ UIComponent.as:1169]       在mx.managers :: LayoutManager / doPhasedInstantiation()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ managers \ LayoutManager.as:718]       在功能/ http://adobe.com/AS3/2006/builtin::apply()       在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]

1 个答案:

答案 0 :(得分:3)

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]);
}

使用ArrayCollection

更容易
arrayCol.addItem(arrayCol.removeItemAt(index));

更新:工作样本 - 亲眼看看。

<?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>