代码之家  ›  专栏  ›  技术社区  ›  Immanuel

移动数组集合中的元素-flex

  •  0
  • Immanuel  · 技术社区  · 15 年前

    有人能引导我在flex中移动数组集合中的元素吗?

    我有一个排序对象的数组集合。

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

    举例说明,

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

    此ArrayCollection已排序。现在我得走了 'Other' 到数组集合的结尾。也就是说,我需要将数组重组为

    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组件::home/creationover()[C:\documents and settings\immanuel\my documents\flex builder 3\porj\src\components\home.mxml:113] AT组件::home/uuuuhome_Canvas1_creationcomplete()[C:\documents and settings\immanuel\my documents\flex builder 3\porj\src\components\home.mxml:2] 在flash.events::EventDispatcher/DispatcheventFunction()时 在flash.events::EventDispatcher/Dispatchevent()时 在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] 在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] 在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);
    

    但是这个,抛出了下面的错误,

    类型错误:错误1006:值不是函数。 在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] 在mx.collections::listcollectionview/listchangehandler()[c:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\listcollectionview.as:1051] 在flash.events::EventDispatcher/DispatcheventFunction()时 在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组件::home/creationover()[C:\documents and settings\immanuel\my documents\flex builder 3\porj\src\components\home.mxml:113] AT组件::home/uuuuhome_Canvas1_creationcomplete()[C:\documents and settings\immanuel\my documents\flex builder 3\porj\src\components\home.mxml:2] 在flash.events::EventDispatcher/DispatcheventFunction()时 在flash.events::EventDispatcher/Dispatchevent()时 在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] 在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] 在mx.core::uicomponent/callLaterDispatcher()[c:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\uicomponent.as:8568]

    1 回复  |  直到 15 年前
        1
  •  3
  •   Amarghosh    15 年前
    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>
    
    推荐文章