2009-04-24 50 views
0

我有一個自定義ItemRenderer的TileList,每個項目顯示一個圖像,它從它從dataProvider接收的數據中提取。奇怪的是,我不知道爲什麼有些項目顯示的圖像不在它們的數據塊中,而是在另一項數據中。如果我從自己的數據中提取圖片url,我不知道如何從另一個項目獲取圖片url。我使用工具提示來顯示圖片網址和項目數據,並驗證該網址不在其數據中。Flex:ListItem從列表中的其他項目中提取數據?

這裏是臨時XML我使用:

<data> 

     <bs item_id="1"> 
      <variation price="300" month="JAN" stone="Garnet" image="<?=$img_dir?>jan.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
      <variation price="400" month="FEB" stone="Garnet" image="<?=$img_dir?>feb.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
      <variation price="550" month="MAR" stone="Garnet" image="<?=$img_dir?>march.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
      <variation price="625" month="APR" stone="Garnet" image="<?=$img_dir?>april.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
     </bs> 

     <bs item_id="2"> 
      <variation price="300" month="JAN" stone="Garnet" image="<?=$img_dir?>jan.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
      <variation price="400" month="FEB" stone="Garnet" image="<?=$img_dir?>feb.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
      <variation price="550" month="MAR" stone="Garnet" image="<?=$img_dir?>march.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
      <variation price="625" month="APR" stone="Garnet" image="<?=$img_dir?>april.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
     </bs> 

     <bs item_id="3"> 
      <variation price="300" month="JAN" stone="Garnet" image="<?=$img_dir?>jan.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
      <variation price="400" month="FEB" stone="Garnet" image="<?=$img_dir?>feb.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
      <variation price="550" month="MAR" stone="Garnet" image="<?=$img_dir?>march.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
      <variation price="625" month="APR" stone="Garnet" image="<?=$img_dir?>april.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
     </bs> 

     <bs item_id="4"> 
      <variation price="300" month="JAN" stone="Garnet" image="<?=$img_dir?>PE105-BT.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
      <variation price="400" month="FEB" stone="Garnet" image="<?=$img_dir?>PE105-EM.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
      <variation price="550" month="MAR" stone="Garnet" image="<?=$img_dir?>PE105-OP.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
      <variation price="625" month="APR" stone="Garnet" image="<?=$img_dir?>PE105.png" style="xsdfcSD" gold_color="Yellow" gold_carat="10"/> 
     </bs> 


</data> 

每個項目得到了< BS>塊。 (4項)

這裏是從ItemRender的代碼:

<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="150" height="150" xmlns:local="*"> 

    <mx:Script> 
     <![CDATA[ 
      import mx.controls.Alert; 

      private var _randomIndex:uint; 
      private var _indexSet:Boolean; 

      private function getRandomImage():String 
      { 
       if (!_indexSet) 
       { 
        var maxIndex:uint = data.children().length(); 
        _randomIndex = Math.floor(Math.random()*maxIndex); 
        _indexSet = true; 
       } 
       return data.children()[_randomIndex][email protected]; 
      } 

     ]]> 
    </mx:Script> 


    <local:LoadingImage id="tn" toolTip="{tn.source+'\n\n'+data}" source="{getRandomImage()}" width="150" height="150"/> 

</mx:Canvas> 

的第二和第三被示出僅在第四塊的圖像。

有沒有人看到我沒有看到的東西?

謝謝!

回答

0

我發現如果我將「數據」傳遞給像getRandomImage(data)這樣的函數,它就解決了這個問題。不是100%確定爲什麼。

+0

原因是未檢測到綁定更改。參見: http://stackoverflow.com/questions/445313/can-i-bind-a-flex-component-property-toa-a-function – an0nym0usc0ward 2009-11-07 00:43:34

1

物料管理員被回收,所以如果你在集合數據()中做任何處理,你應該總是有匹配的if/else語句。您不能假定itemrender中的成員變量處於「已知」狀態。在你的代碼中,它看起來像索引集沒有正確初始化+在if語句中沒有別的。

+0

嗨,感謝您的回覆,我暫時取出了if()語句並將indexSet設置爲始終爲1.所以它應該始終是每個塊的第一個< variation >中的第一個圖像URL。物品2從它自己的圖像來回跳到物品4的圖像。這是我第四次以這種方式使用TileList,從來沒有發生過這種情況。 – 2009-04-24 16:14:06

0

getRandomImage何時被調用? ItemRenderer的被重用,所以他們可能會附加一箇舊的圖像。如果你沒有重新加載一個新的值,它仍然會有相同的圖像。也許你可以嘗試重載dataChanged事件?我也注意到你的_indexSet可能不像你上面所說的那樣工作,但這不應該是你的主要問題。

+0

indexSet的整點是因爲每個塊內部有不同的變化圖像,我想一次選擇一個隨機圖像,但是一旦它被重用,繼續顯示相同的圖像,那麼indexSet是否也會被回收?我仍然困惑。 – 2009-04-24 16:09:42

+2

您需要將indexSet鏈接到您的數據,而不是您的itemRenderer,因爲itemRenderer的狀態在設置數據時未知(indexSet可能爲true或false,具體取決於之前使用過哪些數據,哪些數據沒有知道的方式)。 – CookieOfFortune 2009-04-24 16:22:43