tags:

views:

191

answers:

3

I have a TileList with a custom ItemRenderer, and each item shows an image which it extgracts from the data it receives from the dataProvider. The wierd thing is, and I have no clue why is some items are show images that are not in their data-bock but in another items data. If I am extracting the image url from its own data I have no clue how it can be getting the image urls from another item. I used a tool tip to show the image url and the item's data and verified that the URL is not in its data.

Here is temp XML I am using:

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

Each item gets a < bs > block. (4 items)

And here is the code from the 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].@image;
      }

     ]]>
    </mx:Script>


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

</mx:Canvas>

The 2nd and 3rd are showing images that are only in the 4th block.

Does anyone see something I am not seeing?

Thanks!

+1  A: 

Itemrenderers are recycled, so if you do any processing in set data() you should always have matching if / else statements. You cannot assume that the member variables in the itemrender are in a "known" state. In your code, it looks like indexSet is not being initialized properly + there is no else in the if statement.

an0nym0usc0ward
Hi thanks for your response, I temporarily took out the if() statement and set indexSet to always be 1. so it should always be the first image URL in the fist < variation > of each block. Still item 2 jumps back and forth from its own image to item 4's image. This is my 4th time using TileList in this manner, never had this happen before.
John Isaacks
A: 

When does getRandomImage get called? ItemRenderer's are reused so they may have an old image attached to it. If you don't reload it with a new value, it will still have the same image. Maybe you could try overloading the dataChanged event? I also noticed your _indexSet is probably not working the way you want as stated above, but that shouldn't be your main problem.

CookieOfFortune
The whole point of indexSet is because there are different variation images inside each block, I want to choose a random image once, but keep showing the same image once it is reused, would indexSet get recycled as well? I'm still confused.
John Isaacks
You would need to have indexSet linked to your data, and not your itemRenderer, since the state of the itemRenderer is unknown when it's data is set (indexSet could be true or false, depending on which data was used earlier, which you have no way of knowing).
CookieOfFortune
A: 

I found that if I pass the "data" to the function like this getRandomImage(data) it solves the issue. Not 100% sure why though.

John Isaacks
The reason is that the binding change is not detected. See:http://stackoverflow.com/questions/445313/can-i-bind-a-flex-component-property-to-a-function
an0nym0usc0ward