Flex Spark DropDownList selectedItem 数据源改变后不更新

请先看下面的代码:

<?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"

   creationComplete="flipLast()"

   minWidth="955" minHeight="600">

<fx:Script>

<![CDATA[

     import mx.collections.ArrayCollection;

     public function flipLast():void {

          if( last ) {

               list.dataProvider = dp1;

               list.selectedItem = "Flex";

          } else {

               list.dataProvider = dp2;

               list.selectedItem = "Catalyst";        

          }

          last = !last;

     }



     public var last:Boolean = true;

     public var dp1:ArrayCollection = new ArrayCollection( [ "Flex", "Air" ] );

     public var dp2:ArrayCollection = new ArrayCollection( [ "Catalyst", "FlashBuilder" ] );

]]>

</fx:Script>



<s:VGroup>

     <s:DropDownList id="list" requireSelection="true" />



     <s:Label id="listSelectedItem" text="{list.selectedItem}" />

     <s:Label id="listSelectedIndex" text="{list.selectedIndex}" />



     <s:Button label="Flip" click="flipLast()" />

</s:VGroup>

</s:Application>

当数据源改变的时候列表不更新。

下面是解决方法:

public function flipLast():void {

          list.selectedIndex = -1;

          if( last ) {

               list.dataProvider = dp1;

               list.selectedItem = "Flex";

          } else {

               list.dataProvider = dp2;

               list.selectedItem = "Catalyst";        

          }

          last = !last;

     }

注意这一句:

list.selectedIndex = -1;

你可能感兴趣的:(select)