我正在制作一个小型照片库。我创建了一个xml文件,并尝试使用itemrenderer将其链接到我的List控件。但是,当我尝试保存文件时,我访问了未定义的属性“data”错误。我以为我们假设使用“数据”来引用数据对象的当前行。这是我的代码......非常感谢!
<?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:Declarations>
<fx:Model id="pictureXML" source="data/pictures.xml"/>
<s:ArrayList id="pictureArray" source="{pictureXML.picture}"/>
</fx:Declarations>
<s:List id="pictureGrid" dataProvider="{pictureArray}"
horizontalCenter="0" top="20">
<s:itemRenderer>
<fx:Component>
<s:HGroup>
<mx:Image source="images/big/{data.source}" /> // where the error happen
<s:Label text="{data.caption}"/> // where the error happen
</s:HGroup>
</fx:Component>
</s:itemRenderer>
</s:List>
</s:Application>
我的xml
<?xml version="1.0"?>
<album>
<picture>
<source>city1.jpg </source>
<caption>City View 1</caption>
</picture>
<picture>
<source>city2.jpg </source>
<caption>City View 2</caption>
</picture>
<picture>
<source>city3.jpg </source>
<caption>City View 3</caption>
</picture>
<picture>
<source>city4.jpg </source>
<caption>City View 4</caption>
</picture>
<picture>
<source>city5.jpg </source>
<caption>City View 5</caption>
</picture>
<picture>
<source>city6.jpg </source>
<caption>City View 6</caption>
</picture>
<picture>
<source>city7.jpg </source>
<caption>City View 7</caption>
</picture>
<picture>
<source>city8.jpg </source>
<caption>City View 8</caption>
</picture>
<picture>
<source>city9.jpg </source>
<caption>City View 9</caption>
</picture>
<picture>
<source>city10.jpg </source>
<caption>City View 10</caption>
</picture>
</album>
我感谢任何帮助!!!
答案 0 :(得分:3)
<s:List id="pictureGrid" dataProvider="{pictureArray}"
horizontalCenter="0" top="20">
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer>
<s:HGroup>
<mx:Image source="images/big/{data.source}" /> // where the error happen
<s:Label text="{data.caption}"/> // where the error happen
</s:HGroup>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
试试吧!它会起作用
答案 1 :(得分:1)
我认为您尝试访问的数据属性超出了范围,因为它位于内联itemRenderer中。尝试将ItemRenderer抽象为自己的组件,而不是将其作为内联组件。
您也可以访问内联itemRenderer来访问每个商品数据属性,但这更清晰....
我已将你的IR分成了自己的组件,一切看起来都很好......
<!-- YOUR LIST -->
<s:List id="pictureGrid" dataProvider="{pictureArray}"
horizontalCenter="0" top="20"
itemRenderer="TestRenderer"/> // reference new IR class here
<!-- NEW ITEMRENDERER CLASS -->
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="true">
<s:HGroup>
<mx:Image source="images/big/{data.source}" />
<s:Label text="{data.caption}"/>
</s:HGroup>
</s:ItemRenderer>
答案 2 :(得分:0)
尝试这个
override public function set data(value:Object):void
{
super.data = value;
if (data == null)
return;
irImage.source = data.path;
irText.text = data.caption
}
<s:HGroup>
<mx:Image id="irImage" />
<s:Label id="irText"text="{data.caption}"/>
</s:HGroup>
如果这不起作用,请告诉我。