2010-10-31 24 views
1

在下面的工作示例中,只要文本框發生更改,列表的選定索引應該重置爲0。爲什麼SelectedIndex在Flex 4中每隔一段時間都不工作?

但是,由於某種奇怪的原因,其他擊鍵選定的項目消失,然後再出現在隨後的擊鍵。

<?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.ArrayList; 
      import mx.core.UIComponent; 
      import mx.events.FlexEvent; 

      import spark.effects.Scale; 
      import spark.events.TextOperationEvent; 

      [Bindable] 
      public var items : ArrayList; 

      protected function textinput1_changeHandler(event:TextOperationEvent):void 
      { 
       items = new ArrayList(input.text.split(" ")); 
       list.selectedIndex = 0; 
      } 
     ]]> 
    </fx:Script> 
    <s:TextInput x="165" y="124" change="textinput1_changeHandler(event)" id="input" text="a few words"/> 
    <s:List x="165" y="184" width="433" height="291" dataProvider="{items}" id="list"></s:List> 
</s:Application> 
+3

你嘗試過清除和設置數據提供程序元素,而不只是分配新的每一次擊鍵? – Dan 2010-10-31 18:15:27

+0

是的,這可以解決問題。不清楚爲什麼它應該像給定的代碼一樣行事。 – 2010-11-01 06:07:23

回答

0

首先你應該檢查「String.split」函數。它有幾個錯誤,我不記得它們。按順序嘗試一下,如「」或「blah」(最後的空格)。

此外,您應該等到列表實際更新。更改可綁定屬性只會觸發一些事件,而不會實際更改列表(AFAIK)。只是谷歌列表的事件。您也可以嘗試重寫List的「dataProvider」設置器。

0

問題是,當您設置所選索引時,您的列表尚未呈現。

改變你textinput1_changeHandler方法可以解決這個問題:

protected function textinput1_changeHandler(event:TextOperationEvent):void 
{ 
    items = new ArrayList(input.text.split(" ")); 
    callLater(function():void{list.selectedIndex = 0;}); 
} 
0

添加到您的函數的數據提供商的刷新第一所以它拿起變化:

protected function textinput1_changeHandler(event:TextOperationEvent):void 
{ 
    items = new ArrayList(input.text.split(" ")); 
    (list.dataProvider as ArrayCollection).refresh(); 
    list.selectedIndex = 0; 
} 
0

的原因回四是事件只是與索引更改創建,檢查listbase setselectedindex;

更改selectedIndex設置爲0之前的解決方法是先爲-1,然後將其更改爲0。

/** 
* @private 
* Used internally to specify whether the selectedIndex changed programmatically or due to 
* user interaction. 
* 
* @param dispatchChangeEvent if true, the component will dispatch a "change" event if the 
* value has changed. Otherwise, it will dispatch a "valueCommit" event. 
*/ 
mx_internal function setSelectedIndex(value:int, dispatchChangeEvent:Boolean = false):void 
{ 
    if (value == selectedIndex) 
     return; 

    if (dispatchChangeEvent) 
     dispatchChangeAfterSelection = dispatchChangeEvent; 
    _proposedSelectedIndex = value; 
    invalidateProperties(); 
} 
相關問題