Actionscript 3 警报后在列表中选择索引

Actionscript 3 警报后在列表中选择索引,actionscript-3,apache-flex,Actionscript 3,Apache Flex,我有一个带有ArrayCollection数据提供程序的列表。在我的程序中,有一个按钮,用户可以单击该按钮为列表中的selectedIndex执行功能,但首先会显示一个警报,询问他们是否确定要执行该操作。用户回答警报后,将对列表中的selectedIndex执行操作 我的问题是在警报窗口关闭事件之后选择了dex=-1,即使它已被明确选中。我通过对Alert CloseEvent代码中的列表执行validateNow()来解决这个问题 我的问题:为什么我必须这样做,我做错了什么?或者这是正常/最佳

我有一个带有ArrayCollection数据提供程序的列表。在我的程序中,有一个按钮,用户可以单击该按钮为列表中的selectedIndex执行功能,但首先会显示一个警报,询问他们是否确定要执行该操作。用户回答警报后,将对列表中的selectedIndex执行操作

我的问题是在警报窗口关闭事件之后选择了dex=-1,即使它已被明确选中。我通过对Alert CloseEvent代码中的列表执行validateNow()来解决这个问题

我的问题:为什么我必须这样做,我做错了什么?或者这是正常/最佳做法?此外,除了使用try-catch外,是否还有更好的/最佳的做法来检查列表,以查看是否选择了某些内容。如果未选择任何内容,我不希望最终用户看到生成的错误

代码:


因此,在上面的代码中,如果取出validateNow(),将引发一个异常,因为它认为selectedIndex是-1。

我会这样做:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;
        import mx.events.CloseEvent;

        [Bindable]private var friendsList:ArrayCollection = new ArrayCollection([{data:"111", label:"Don"}, {data:"222", label:"John"}]);

        private function onBtnRemove():void
        {
            laFriend.text = "";

            try 
            {
                if (cbFriends.selectedIndex != -1) 
                {
                    Alert.show("Are you sure you want to remove " + cbFriends.selectedItem.label + " as a friend?", "Remove Friend", Alert.YES | Alert.CANCEL, this, this._removeFriendConfirm, null, Alert.CANCEL);
                }
            } catch (e:Error) { }
        }

        private function _removeFriendConfirm(event:CloseEvent):void
        {
            laFriend.text = "Selected friend: " + cbFriends.selectedItem.label;
        }
    ]]>
</fx:Script>


<mx:VBox>
    <s:ComboBox id="cbFriends" dataProvider="{friendsList}"/>
    <s:Button id="btnRemove" label="Remove" click="onBtnRemove()"/>
    <s:Label id="laFriend" text=""/>
</mx:VBox>

</s:Application>

在调用处理程序之前是否执行选择

如果设置selectedIndex,则无法立即获取它,因为livecycle-在读取它之前,必须提交值


您的validateNow强制提交。但是,稍后将在不手动执行的情况下执行。

这不是正常行为,但您显示的代码不足以评估发生了什么。您必须给我们更多的工作空间。为什么不在警报窗口之前保存所选索引?@RIAstar我不确定还要包括什么?@Anton我想我可以,但我必须创建另一个变量……您使用的是什么版本的Flex?我接受了@Anton提供的代码,它可以工作。您不需要另一个变量。我使用的是Flex 4.6,在
\u removeFriendConfirm()
事件处理程序中,
selectedIndex
从不为-1。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;
        import mx.events.CloseEvent;

        [Bindable]private var friendsList:ArrayCollection = new ArrayCollection([{data:"111", label:"Don"}, {data:"222", label:"John"}]);

        private function onBtnRemove():void
        {
            laFriend.text = "";

            try 
            {
                if (cbFriends.selectedIndex != -1) 
                {
                    Alert.show("Are you sure you want to remove " + cbFriends.selectedItem.label + " as a friend?", "Remove Friend", Alert.YES | Alert.CANCEL, this, this._removeFriendConfirm, null, Alert.CANCEL);
                }
            } catch (e:Error) { }
        }

        private function _removeFriendConfirm(event:CloseEvent):void
        {
            laFriend.text = "Selected friend: " + cbFriends.selectedItem.label;
        }
    ]]>
</fx:Script>


<mx:VBox>
    <s:ComboBox id="cbFriends" dataProvider="{friendsList}"/>
    <s:Button id="btnRemove" label="Remove" click="onBtnRemove()"/>
    <s:Label id="laFriend" text=""/>
</mx:VBox>

</s:Application>