2012-12-17 40 views
0

我有一个ArrayCollection数据提供者的列表。在我的程序中,有一个按钮,用户可以单击以执行List的selectedIndex函数,但是会先显示一个Alert,询问他们是否确定要执行此操作。用户应答警报后,将对列表的selectedIndex执行操作。selectedindex in Alert after Alert

我的问题是,在Alert窗口CloseEvent之后selectedIndex = -1,即使它被明确选中。我通过在Alert CloseEvent的代码列表中执行validateNow()来解决这个问题。

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

代码:

//Note: "fl" is a class with "friendsList" bindable ArrayCollection; for the sake of keeping this short I will not include it 

private function _removeFriendClick(event:MouseEvent):void 
{ 
    try { 
     if (this.friendsList.selectedIndex != -1) { 
      Alert.show("Are you sure you want to remove "+this.fl.friendsList[this.friendsList.selectedIndex].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 
{ 
    this.friendsList.validateNow(); 
    trace(this.friendsList.selectedIndex); 
} 

因此,与上面的代码,如果你拿出validateNow(),抛出一个异常,因为它认为将selectedIndex为-1。

+1

这不是正常行为,但是您显示的代码不足以评估正在发生的事情。你将不得不再给我们一些工作。 – RIAstar

+0

为什么您不想在警报窗口之前保存选定的索引? – Anton

+0

@RIAstar我不太清楚还有什么可以包含的? – Lyynk424

回答

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

你完成选择?

如果将selectedIndex设置,你不能拿回来马上因为的LiveCycle的 - 之前,你可以读取它的值应COMMITED。

您的validateNow力量提交。但是,它稍后会发生,而不会手动强制执行。