1

我有一个具有以下结构的arrayCollection:

projectErrorsAC
    0
        project1number
        project2number
        position1number
        position2number
        project1name
        project2name
        student
    1
        ...

AC 定义如下:

[Bindable] private var projectErrorsAC:ArrayCollection = new ArrayCollection;

我在中继器中使用这个交流电来显示每个错误。在显示每个错误后,我放置了一个“接受”和“拒绝”按钮。一旦用户单击这些按钮中的任何一个,我想调用一个从 AC 中删除特定错误的函数。这是我到目前为止所拥有的:

中继器:

<mx:Repeater id="projRP" width="100%" dataProvider="{projectErrorsAC}">
<mx:HBox>
    <mx:Text id="projmsg" text="{projRP.currentItem.student} is working on the following projects on the same day: {projRP.currentItem.proj1name} and {projRP.currentItem.proj2name}." />
    <mx:Text id="allow" text="Allow" color="#ff0000" selectable="false" 
        click="acceptProjConflict(projRP.currentItem);" 
        mouseOver="parentApplication.switchCursor(true);" 
        mouseOut="parentApplication.switchCursor(false);" />
    <mx:Text text=" |" />
    <mx:Text id="decline" text="Decline" color="#ff0000" selectable="false" click="declineProjConflict(projRP.currentItem);" mouseOver="parentApplication.switchCursor(true);" mouseOut="parentApplication.switchCursor(false);" />
</mx:HBox>
</mx:Repeater>

这是我在“点击”部分调用的函数:

public function acceptProjConflict(conflict:Object):void
{
for (var i:int = 0; i < projectErrorsAC.length; i++)
{
    if (projectErrorsAC.getItemAt(i) == conflict)
        projectErrorsAC.removeItemAt(i);
}               
}

由于某种原因,这不起作用...

* 编辑 *

成功!

我必须创建一个模块来放入中继器 - 中继器现在看起来像这样:

<mx:Repeater id="projRP" width="100%" dataProvider="{projectErrorsAC}">
    <conflict:showErrors id="projErrors" thisObject="{projRP.currentItem}" isProject="true"/>
</mx:Repeater>

我的模块看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
        <mx:Script>
            <![CDATA[
                public var isProject:Boolean;
                public var thisObject:Object;
                [Bindable] public var displayString:String = new String;

                private function init():void
                {
                    if (isProject)
                    {
                        displayString = thisObject.student + " is working on the following projects on the same day: " + thisObject.proj1name + " and " + thisObject.proj2name + ".";
                    }
                }
            ]]>
        </mx:Script>

    <mx:Canvas width="750">
        <mx:HBox>
            <mx:Text id="projmsg" text="{displayString}" />
            <mx:Text id="allow" text="Allow" color="#ff0000" selectable="false" click="parentDocument.acceptProjConflict(thisObject)" mouseOver="parentApplication.switchCursor(true);" mouseOut="parentApplication.switchCursor(false);" />
            <mx:Text text=" |" />
            <mx:Text id="decline" text="Decline" color="#ff0000" selectable="false" click="parentDocument.declineProjConflict(thisObject);" mouseOver="parentApplication.switchCursor(true);" mouseOut="parentApplication.switchCursor(false);" />
        </mx:HBox>
    </mx:Canvas>
</mx:Module>
4

3 回答 3

0

首先,您是否尝试过调试?

其次,我对 click 处理程序的工作感到惊讶,因为 Repeater 有奇怪的范围问题(通常会默默地失败),这就是为什么我说你应该尝试调试的原因。

第三,您的点击处理程序效率低下,您应该这样做:

public function acceptProjConflict(conflict:Object):void
{
   for(var i:uint, len:uint = projectErrorsAC.length; i<len; i++)
   {
       if(projectErrorsAC.source[i] == conflict)
       {
           projectErrorsAC.removeItemAt(i);
           break;
       }
   }         
}

但是,是的,相当肯定你的问题在于你的处理程序从未被调用过。

于 2011-05-18T15:46:06.330 回答
0

这可能是导致的特定于 ArrayCollection 的问题,因为您可能会尝试删除不再位于您认为可能的索引处的对象。我相信这类问题正是 Java 建议使用集合迭代器的原因。

进一步的解释在这里

此外,我建议您将 List 与 itemRenderer 而不是中继器一起使用。中继器以其导致内存泄漏的能力而闻名,并且不如使用 itemRenderers 的 List 进行优化。

干杯

于 2011-05-18T15:31:31.197 回答
0

你可能会使用

[Bindable] private var projectErrorsAC:ArrayCollection = []; 

代替

[Bindable] private var projectErrorsAC:ArrayCollection = new ArrayCollection;

当你有大数据时它会更快。

于 2013-09-03T19:40:14.800 回答