2010-05-09 50 views
2

我正在处理一个小图片库。我创建一个xml文件,并尝试使用itemrenderer将其链接到我的List控件。但是,当我试图保存文件时,我得到了未定义属性“数据”错误的访问权限。我认为我们假设使用“数据”来引用数据对象的当前行。这是我的代码...并且非常感谢!Flex列表控件itemrenderer问题

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

错误只发生在mx:图像行或mx:图像和s:标签? – NKijak 2011-07-11 17:53:55

回答

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

我认为你试图访问的数据属性超出了范围,因为它在一个内嵌的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> 
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> 

如果不工作,然后告诉我。