2010-05-09 138 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> 

如果不工作,然後告訴我。