PDA

View Full Version : mxml to actionscript - assign object array value


janicep
12-11-2007, 02:35 PM
:confused: I am so close I can taste it, but there is something wrong with my code and I can't figure it out at all. The samples all over adobe only show how to assign an array through mxml. What I want to know is the proper syntax in passing array data to an object when using a List and ListItemRenderer.

here's what I have so far....in my original action script class called "Legend.as" (extends a canvas) I have this list object:

public function addLegendList():void
{
this._legendList = new List();
this._legendList.x = 3;
this._legendList.y = 41;
this._legendList.width =this._leftCanvas.width-8;
this._legendList.height = this._leftCanvas.height - 53;
this._legendList.id = "legendList";
this._legendList.variableRowHeight = true;
this._legendList.dataProvider = this._atlasLegendData;
this._legendList.itemRenderer=new ClassFactory(morris.tools.LegendListItemRenderer);

this._leftCanvas.addChild(_legendList);

}

The dataProvider above is the following array stored in Legend.as(stripped way down because I'm having trouble)....


public var _atlasLegendData:Array = new Array
(
{label:"HELLO", IsVisible:"true", IsSelected:"false", layerThumbnailSource:"IMAGE1"},
{label:"HELLO AGAIN",IsVisible:"true", IsSelected:"false", layerThumbnailSource:"IMAGE2"},
{label:"AND HELLO AGAIN!",IsVisible:"true", IsSelected:"false", layerThumbnailSource:"IMAGE3"}
);


My ListItemRenderer is another actionscript class called "LegendListItemRenderer.as" (It was much more complex but I've stripped it down because nothing's working!!!!) that renders a layer name in purple. I'm getting stuck below (in red)....

public function addLayerTextArea():void
{
this._layerTextArea = new TextArea();
this._layerTextArea.x = 78;
this._layerTextArea.y = 2;
this._layerTextArea.maxHeight = 50;
this._layerTextArea.maxWidth =108;
this._layerTextArea.selectable = false;
this._layerTextArea.id = "layerTextArea";
this._layerTextArea.setStyle("fontSize","9");
this._layerTextArea.setStyle("color","#D400FF");

[B]this._layerTextArea.text = (data.label);
//THIS IS WHERE I GET STUCK!!!! My syntax is probably wrong, but I have no idea how to fix it. If I do this, my application will build, but won't run. I know the list is reading my array (tested with this._legendList.labelField="label"; and it will put my labels in from the array, but I can't get the ListItemRenderer to get the array data without bombing out my app).

this.addChild(_layerTextArea);
}

I can also see my ListItemRenderer if I pre-set a text value instead of trying to pass the data to it.... this._layerTextArea.text = "PLEASE WORK!!!".
How does one pass data to an object in an ItemListRenderer from an array stored in the parent app/class?

Any help would be incredibly helpful! I've been stuck on this for HOURS!!!!!!!

MANY THANKS IN ADVANCE!

Jim Freer
12-11-2007, 03:44 PM
What is your custom ListItemDataRenderer class based on? In order to have the property “data” available it must implement the IDataRenderer interface. You have a child TextArea which does implement IDataRenderer but unfortunately that is unknown to the list control.

I haven’t looked any closer into your project at this point but this may be a place to start in solving your problem.

Jim Freer
http://freerpad.blogspot.com/

janicep
12-11-2007, 04:07 PM
My LegendListItemRenderer.as extends a Canvas.....

import mx.containers.Canvas;
import mx.controls.RadioButton;
import mx.controls.CheckBox;
import mx.controls.TextArea;
import flash.display.Bitmap;
import mx.controls.Image;
import mx.core.IDataRenderer;
import mx.controls.listClasses.ListItemRenderer;

public dynamic class LegendListItemRenderer extends Canvas
{........

Jim Freer
12-11-2007, 04:28 PM
I loaded your code into Flex and made a few changes to get it to compile. I assumed the datarenderer is based on a Canvas. Here are the changes I made to get it to work. Basically added the data property code and moved the code to set the text to the set data function.


// ---------------------------------------------------------------------
// addLayerTextArea
// ---------------------------------------------------------------------

public function addLayerTextArea
()
:void
{
_layerTextArea = new TextArea();
_layerTextArea.x = 78;
_layerTextArea.y = 2;
_layerTextArea.maxHeight = 50;
_layerTextArea.maxWidth =108;
_layerTextArea.selectable = false;
_layerTextArea.id = "layerTextArea";
_layerTextArea.setStyle("fontSize","9");
_layerTextArea.setStyle("color","#D400FF");

addChild(_layerTextArea);

} // addLayerTextArea

// ---------------------------------------------------------------------
// data
// ---------------------------------------------------------------------

public override function set data
( avObject :Object )
:void
{
_layerTextArea.text = avObject.label;

} // set data


Jim Freer
http://freerpad.blogspot.com/

janicep
12-11-2007, 04:55 PM
JIM!!! YOU ARE A LIFE SAVER!!! THAT WORKED!

I'm new to actionscript, so perhaps I missed some crucial elements to your first reply.

THANK YOU!!!! I'm now going to run with this with my expanded array!

again, MANY MANY THANKS!!!!!!

Jim Freer
12-11-2007, 11:19 PM
Although it shouldn’t make any difference in this instance the documented way to override the set data function is as follows:


// ---------------------------------------------------------------------
// data
// ---------------------------------------------------------------------

public override function set data
( value :Object )
:void
{
if( value != null )
{
super.data = value;
_layerTextArea.text = value.label;
} // if

} // set data


The Adobe Flex 3 Developer’s Guide (a PDF) has a wealth of information on data renderers. Unfortunately there are so many ways to accomplish the same thing it is very repetitive. I use to print out the Adobe manuals but this one is almost 2000 pages. The problem is it is sometimes easy to forget it exists.

Jim Freer
http://freerpad.blogspot.com/