23 May 2011
Beginning
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).
In this section, you will use the DefaultItemRenderer class to render a group of data.
Declarations comment.Declarations tag block, create a set of ArrayList tags:<fx:Declarations>
<s:ArrayList>
</s:ArrayList>
</fx:Declarations>
ArrayList collection, add the id property with a value of employeeList:<s:ArrayList id="employeeList">
</s:ArrayList>
ArrayList tags, create a String block:<s:ArrayList id="employeeList">
<fx:String></fx:String>
</s:ArrayList>
String class:<s:ArrayList id="employeeList">
<fx:String>Samuel Ang</fx:String>
</s:ArrayList>
String tags and paste three more instances within the ArrayList collection below the first String class instance:<s:ArrayList id="employeeList">
<fx:String>Samuel Ang</fx:String>
<fx:String>Samuel Ang</fx:String>
<fx:String>Samuel Ang</fx:String>
<fx:String>Samuel Ang</fx:String>
</s:ArrayList>
String instance values to the following employee names:Athena Parker, Saul Tucker, and Alyssa Le.<s:ArrayList id="employeeList">
<fx:String>Samuel Ang</fx:String>
<fx:String>Athena Parker</fx:String>
<fx:String>Saul Tucker</fx:String>
<fx:String>Alyssa Le</fx:String>
</s:ArrayList>
Label control below the UI components comment:Label control, create a DataGroup component tag block:<s:Label width="440" height="40"
text="Employee Portal: Employee Directory"
styleName="titleHeader" />
<s:DataGroup>
</s:DataGroup>
DataGroup component, add the dataProvider property and bind the value to the employeeList ArrayList collection.:<s:DataGroup dataProvider="{employeeList}">
</s:DataGroup>
You should see an ActionScript error message, "Could not create an item renderer for Samuel Ang" (see Figure 2).
DataGroup component, assign spark.skins.spark.DefaultItemRenderer as the itemRenderer class:<s:DataGroup dataProvider="{employeeList}"
itemRenderer="spark.skins.spark.DefaultItemRenderer">
</s:DataGroup>
Note that the employee names in the ArrayList collection are all displayed in the same place (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.
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>
VerticalLayout tag, add the paddingLeft and the paddingTop properties, both with a value of 25.<s:layout>
<s:VerticalLayout paddingLeft="25"
paddingTop="25"/>
</s:layout>
You should see the employee names displayed vertically below the header text (see Figure 4).
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 a BitmapImage control:...
<s:ArrayList id="employeeList">
<fx:String>Samuel Ang</fx:String>
<fx:String>Athena Parker</fx:String>
<fx:String>Saul Tucker</fx:String>
<fx:String>Alyssa Le</fx:String>
<s:BitmapImage/>
</s:ArrayList>
...
BitmapImage control, add the source property with a value of images/sang.jpg....
<fx:String>Alyssa Le</fx:String>
<s:BitmapImage source="images/sang.jpg"/>
</s:ArrayList>
...
ArrayList collection, copy the BitmapImage control and paste three instances below the first:...
<s:BitmapImage source="images/sang.jpg"/>
<s:BitmapImage source="images/sang.jpg"/>
<s:BitmapImage source="images/sang.jpg"/>
<s:BitmapImage source="images/sang.jpg"/>
</s:ArrayList>
...
source properties of the added BitmapImage controls to use the following images from the images directory: aparker.jpg, stucker.jpg, and ale.jpg....
<s:BitmapImage source="images/sang.jpg"/>
<s:BitmapImage source="images/aparker.jpg"/>
<s:BitmapImage source="images/stucker.jpg"/>
<s:BitmapImage source="images/ale.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.
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, add the itemRendererFunction property with a value of rendererFunction:<s:DataGroup dataProvider="{employeeList}"
itemRendererFunction="rendererFunction">
<s:layout>
<s:VerticalLayout/>
</s:layout>
</s:DataGroup>
ArrayList collection. 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
{
}
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, type if, invoke content assist and select the if statement template to create a condition 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 the content assist tool to return the DefaultItemRenderer class and note that the corresponding 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 the content assist tool to select the DefaultComplexItemRenderer as the class to be returned and note that the corresponding 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 collection (see Figure 6).
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 collection nested within the Declarations block.ArrayList collection, move the BitmapImage controls so they are located below the respective String tag.<s:ArrayList id="employeeList">
<fx:String>Samuel Ang</fx:String>
<s:BitmapImage source="images/sang.jpg"/>
<fx:String>Athena Parker</fx:String>
<s:BitmapImage source="images/aparker.jpg"/>
<fx:String>Saul Tucker</fx:String>
<s:BitmapImage source="images/stucker.jpg"/>
<fx:String>Alyssa Le</fx:String>
<s:BitmapImage source="images/ale.jpg"/>
</s:ArrayList>
You should see the employees names are displayed above their picture (see Figure 7).
In this section, you will use the ClassFactory class to instantiate a custom item renderer. You will learn more about item renderers in the next video and associated exercise.
rendererFunction() method in the Script block. if statement within the function, change the return operation so that it returns a new instance of the ClassFactory class and renders the data using the NameDisplay class. Use the content assist tool (CTRL+Space) to select the NameDisplay as the class to be returned and note that the corresponding import statement is generated within the Script block.if(item is String)
{
return new ClassFactory(NameDisplay);
}
You should see the data from the custom item renderer displayed (see Figure 8).
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. In the next exercise you will create a custom item renderer and use it to display employee data.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.