最近刚接触FLEX,尝试第一个小例子就出现了如下错误:【1061: 调用可能未定义的方法 addItem (通过 static 类型 spark.components:List 引用)】
通过查询,原料flex3 中的list控件已经没有了addItem方法,而是使用ArrayCollection这个东东。下面贴出完整的例子代码,作为像我一样的新手的参考。
希望我们一起在flex的学习道理上越走越远。
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
<!-- data model -->
<mx:Array id="coffeeArray">
<mx:Object>
<mx:label>Red Sea</mx:label>
<mx:data>Smooth and fragrant</mx:data>
</mx:Object>
<mx:Object>
<mx:label>Andes</mx:label>
<mx:data>Rich and pungent</mx:data>
</mx:Object>
<mx:Object>
<mx:label>Blue Mountain</mx:label>
<mx:data>Delicate and refined</mx:data>
</mx:Object>
</mx:Array>
<mx:Script>
<![CDATA[
import mx.controls.List;
import mx.collections.ArrayCollection;
function addToCart():void
{
if(cart.dataProvider==null){
cart.dataProvider=new ArrayCollection();
}
ArrayCollection(cart.dataProvider).addItem(coffeeCombo.selectedItem.label);
}
]]>
</mx:Script>
<!-- view -->
<mx:Panel title="My First Flex App">
<mx:Label text="Coffee Blends"/>
<mx:ComboBox id="coffeeCombo" dataProvider="{coffeeArray}"/>
<mx:Text text="Description: {coffeeCombo.selectedItem.data}"/>
<mx:Button label="Add to Cart" click="addToCart()"/>
<mx:List id="cart" />
</mx:Panel>
</mx:Application>