In this exercise, you will render data in a DataGroup container. You will first do so with simple string data and then UI elements. Lastly, you will display both strings and images in an item renderer that employs a function and conditional evaluation (see Figure 1).

Figure 1. Review your task for this exercise.
In this exercise, you will learn how to:
In order to complete this tutorial, you need the following software and files:
In this section, you will use the DefaultItemRenderer class to render a group of data.
Declarations comment.Declarations comment, create a Declarations tag set:
<!-- Declarations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<fx:Declarations>
</fx:Declarations>
Declarations tag block, create a set of ArrayList tags:
<fx:Declarations>
<s:ArrayList>
</s:ArrayList>
</fx:Declarations>
ArrayList instance, assign employeeList as the id:
<s:ArrayList id="employeeList">
</s:ArrayList>
ArrayList tags, create a String tag set:
<s:ArrayList id="employeeList">
<fx:String></fx:String>
</s:ArrayList>
String class:
<s:ArrayList id="employeeList">
<fx:String>Andrew Brilliam</fx:String>
</s:ArrayList>
String tags and paste five instances within the ArrayList instance below the first String class instance:
<s:ArrayList id="employeeList">
<fx:String>Andrew Brilliam</fx:String>
<fx:String>Andrew Brilliam</fx:String>
<fx:String>Andrew Brilliam</fx:String>
<fx:String>Andrew Brilliam</fx:String>
<fx:String>Andrew Brilliam</fx:String>
<fx:String>Andrew Brilliam</fx:String>
</s:ArrayList>
String instance values to the following employee names: Ben Crater, David Avenon, Annette Kotter, Brian Minnows, and Barry Kramson:
<s:ArrayList id="employeeList">
<fx:String>Andrew Brilliam</fx:String>
<fx:String>Ben Crater</fx:String>
<fx:String>David Avenon</fx:String>
<fx:String>Annette Kotter</fx:String>
<fx:String>Brian Minnows</fx:String>
<fx:String>Barry Kramson</fx:String>
</s:ArrayList>
Label control below the UI components comment:Label control, create a DataGroup component tag block:
<s:Label text="Employee Directory"
color="#00748D"
fontSize="24"/>
<s:DataGroup>
</s:DataGroup>
DataGroup component, bind employeeList as the dataProvider:
<s:DataGroup dataProvider="{employeeList}">
</s:DataGroup>You should see an ActionScript error message, "Could not create an item renderer for Andrew Brilliam" (see Figure 2).

Figure 2. Run the application to see the ActionScript error message.
DataGroup component, assign spark.skins.spark.DefaultItemRenderer as the itemRenderer class:
<s:DataGroup dataProvider="{employeeList}"
itemRenderer="spark.skins.spark.DefaultItemRenderer">
</s:DataGroup>You should see the last employee in the ArrayList collection displayed below the header text (see Figure 3). All the employee names have been processed, but because no layout has been assigned to the DataGroup component, each employee name has been assigned the default x and y values of zero, which causes them to be stacked on top of each other.

Figure 3. Run the application to see the data displayed.
DataGroup tags, create a set of layout property tags and assign a VerticalLayout instance:
<s:DataGroup dataProvider="{employeeList}"
itemRenderer="spark.skins.spark.DefaultItemRenderer">
<s:layout>
<s:VerticalLayout/>
</s:layout>
</s:DataGroup>You should see the employee names displayed vertically below the header text (see Figure 4).

Figure 4. Run the application to see the employees displayed vertically below the header text.
In this section, you will use the DefaultComplexItemRenderer class to render employee image data.
ArrayList collection nested within the Declarations tag block.String instance, create an Image control:
...
<s:ArrayList id="employeeList">
<fx:String>Andrew Brilliam</fx:String>
<fx:String>Ben Crater</fx:String>
<fx:String>David Avenon</fx:String>
<fx:String>Annette Kotter</fx:String>
<fx:String>Brian Minnows</fx:String>
<fx:String>Barry Kramson</fx:String>
<mx:Image/>
</s:ArrayList>
...
Image control, assign images/abrilliam.jpg as the source:
...
<fx:String>Barry Kramson</fx:String>
<mx:Image source="images/abrilliam.jpg"/>
</s:ArrayList>
...
ArrayList collection, copy the Image control and paste five instances below the first:
...
<mx:Image source="images/abrilliam.jpg"/>
<mx:Image source="images/abrilliam.jpg"/>
<mx:Image source="images/abrilliam.jpg"/>
<mx:Image source="images/abrilliam.jpg"/>
<mx:Image source="images/abrilliam.jpg"/>
<mx:Image source="images/abrilliam.jpg"/>
</s:ArrayList>
...
Image controls to use the following images from the images directory: bcrater.jpg, davenon.jpg, akotter.jpg, bminnows.jpg, and bkramson.jpg:
...
<mx:Image source="images/abrilliam.jpg"/>
<mx:Image source="images/bcrater.jpg"/>
<mx:Image source="images/davenon.jpg"/>
<mx:Image source="images/akotter.jpg"/>
<mx:Image source="images/bminnows.jpg"/>
<mx:Image source="images/bkramson.jpg"/>
</s:ArrayList>
...
DataGroup component.DataGroup component, reassign spark.skins.spark.DefaultComplexItemRenderer as the itemRenderer:
<s:DataGroup dataProvider="{employeeList}"
itemRenderer="spark.skins.spark.DefaultComplexItemRenderer">
<s:layout>
<s:VerticalLayout/>
</s:layout>
</s:DataGroup>
You should see the employee images displayed vertically below the header (see Figure 5). Note that the employee names are not displayed.

Figure 5. Run the application to see the employee images displayed, but not the employee names.
In this section, you will use a function to determine the correct item renderer class to use to render the data.
DataGroup component.DataGroup component, remove the itemRenderer property and value.DataGroup component, assign rendererFunction as the itemRendererFunction value:
<s:DataGroup dataProvider="{employeeList}"
itemRendererFunction="rendererFunction">
<s:layout>
<s:VerticalLayout/>
</s:layout>
</s:DataGroup>
You will now create the function that tests what type of data is being rendered for each element in the ArrayList instance. Remember that this function is called multiple times, once for every element of data.
Script comment.Script block:
<!-- Script ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <fx:Script>
<![CDATA[
]]>
</fx:Script>
Script block, create a private function named rendererFunction that returns information data typed to the ClassFactory class:
private function rendererFunction():ClassFactory
{
}For the rendererFunction() function, assign one parameter named item and data type it to the Object class:
private function rendererFunction(item:Object):ClassFactory
{
}<
The item argument represents the data element that is currently being evaluated by this rendererFunction() method. If the data is a string, then you will use the DefaultItemRenderer class to render it. If the data is a UI element, then you will use the DefaultComplexItemRenderer to render it.
rendererFunction() function, create an if statement that evaluates if the item parameter value is of the String data type:
private function rendererFunction(item:Object):ClassFactory
{
if(item is String)
{
}
}if statement, use the return statement to create a new instance of the ClassFactory class and render the data using the DefautItemRenderer class. Use code completion for the DefaultItemRenderer and note that the following import statement is generated within the Script block:
import spark.skins.spark.DefaultItemRenderer;
if(item is String)
{
return new ClassFactory(DefaultItemRenderer);
}if statement, create an else statement:
if(item is String)
{
return new ClassFactory(DefaultItemRenderer);
}
else
{
}else statement, use the return statement to return a new instance of the ClassFactory class and render the data using the DefaultComplexItemRenderer class. Use code completion for the DefaultComplexItemRenderer and note that the following import statement is generated within the Script block:
import spark.skins.spark.DefaultComplexItemRenderer;
if(item is String)
{
return new ClassFactory(DefaultItemRenderer);
}
else
{
return new ClassFactory(DefaultComplexItemRenderer);
}You should see the employee names and images are displayed in the order they are listed within the ArrayList instance (see Figure 6).

Figure 6. Run the application to see all the data displayed.
In this section, you will arrange the data objects in the ArrayList collection so that the employee names and images are displayed relative to each other.
ArrayList instance nested within the Declarations block.ArrayList instance, move the Image controls so they are located below the respective String tag.
<s:ArrayList id="employeeList">
<fx:String>Andrew Brilliam</fx:String>
<mx:Image source="images/abrilliam.jpg"/>
<fx:String>Ben Crater</fx:String>
<mx:Image source="images/bcrater.jpg"/>
<fx:String>David Avenon</fx:String>
<mx:Image source="images/davenon.jpg"/>
<fx:String>Annette Kotter</fx:String>
<mx:Image source="images/akotter.jpg"/>
<fx:String>Brian Minnows</fx:String>
<mx:Image source="images/bminnows.jpg"/>
<fx:String>Barry Kramson</fx:String>
<mx:Image source="images/bkramson.jpg"/>
</s:ArrayList>You should see the employees names are displayed above their picture (see Figure 7).

Figure 7. Run the application to see the employee names displayed above their images.
In this exercise, you learned how to use the DefaultItemRenderer and DefaultComplexItemRenderer classes to render data for a DataGroup component. You also learned how to create a function to render data.
String type.itemRendererFunction property.Trilemetry, Inc is a development and education organization that implements a human-centered design approach to the creation of software and content. Their Adobe portfolio includes the Adobe ColdFusion Getting Started Experience, the Adobe Flex Getting Started Experience, the Flex in a Week video series, the official Adobe instructor-led training course Flex 3: Extending and Styling Components and more. They also create and support many Web applications from interactive Flash sites and corporate web sites to mission-critical business applications.