May be this is very simple but right now I am unable to put a solution for it. Here is brief description of my problem:
I have a dictionary of clipart objects as:
clipartDict['cat'] = Cat; //Cat.mxml
clipartDict['dog'] = Dog; //Dog.mxml
Cat.mxml:
<s:Graphic>
<s:Path x="2.86723" y="-0.000106812" data="M3.45943 80.3419C3.06051 77.3605 0.002399>
</s:Path>
</s:Graphic>
MyView.mxml (relevant code):
<s:SkinnableDataContainer width="300" dataProvider="{clipArts}">
<s:layout>
<s:TileLayout requestedColumnCount="1"/>
</s:layout>
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer>
<fx:Script>
<![CDATA[
import models.vo.ClipArtVO;
// (data as ClipArtVO).clipArtFileName represents the 'key' in dictionary.
// Now, how can I display the relevent clipart from dict based on the key
// this.addElement throws an Type Coercion error
]]>
</fx:Script>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:SkinnableDataContainer>
Can anyone suggest me a solution or any idea to implement it in any different way?
Thanks.
What you’ve put in that Dictionary are Class references and not instances. You’ll have to create an instance of the desired Graphic to add it to the displayList. So there are two methods to fix your issue.
Method 1
Put instances of the Graphics in the Dictionary (instead of Class references):
Then simply add it to the displayList:
Method 2
Create an instance of the Class reference on the fly. We keep the Dictionary as it was:
and create an instance and add it to the displayList like this: