Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > Flex > Flex 2, 3 & 4

Reply
 
Thread Tools Rate Thread Display Modes
Old 04-04-2012, 08:57 PM   #1
Blkhwks19
Member
 
Join Date: Oct 2006
Posts: 85
Default Auto-generated itemrenderer has an error

Im loading in some XML data into a Spark DataGrid. Everything works properly. Now I want to add a custom itemrenderer to one of the columns. I have Flash Builder auto-generate the itemrenderer and simply run the code, but the custom item renderer throws an error. Here's all the code Im working with:

Main.mxml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="creationCompleteHandler(event)">
	
	<fx:Declarations>
		<s:HTTPService id="service" url="data.xml" resultFormat="e4x" result="resultHandler(event)" fault="faultHandler(event)"/> 
	</fx:Declarations>
	
	<fx:Script>
		<![CDATA[
			import mx.collections.XMLListCollection;
			import mx.controls.Alert;
			import spark.components.gridClasses.GridColumn;
			import mx.events.FlexEvent;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			
			private var xml:XML;
			private var xmlList:XMLList;
			private var xmlListColl:XMLListCollection
						
			protected function creationCompleteHandler(event:FlexEvent):void
			{
				service.send();
			}
			
			protected function resultHandler(event:ResultEvent):void
			{
				xml = event.result as XML;
				xmlList = new XMLList(xml);
				xmlListColl = new XMLListCollection(xmlList.game);
				dg.dataProvider = xmlListColl;
			}
						
			protected function faultHandler(event:FaultEvent):void
			{
				trace("There was an error loading the XML.");
			}
			
		]]>
	</fx:Script>
	
	<s:DataGrid id="dg" width="500" rowHeight="30">
		<s:columns>
			<s:ArrayList>
				<s:GridColumn headerText="Col1" dataField="@attr1"/>
				<s:GridColumn headerText="Col2" dataField="@attr2"/>
				<s:GridColumn headerText="Col3" dataField="@attr3"/>
				<s:GridColumn headerText="Col4" dataField="@attr4" itemRenderer="CustomRenderer"/>
                       </s:ArrayList>
		</s:columns>
	</s:DataGrid>
</s:Application>
CustomRenderer.mxml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
					xmlns:s="library://ns.adobe.com/flex/spark" 
					xmlns:mx="library://ns.adobe.com/flex/mx" clipAndEnableScrolling="true">
	
	<fx:Script>
		<![CDATA[
			override public function prepare(hasBeenRecycled:Boolean):void {
				lblData.text = data[column.dataField]
			}
		]]>
	</fx:Script>
	
	<s:Label id="lblData" top="9" left="7"/>
	
</s:GridItemRenderer>

data.xml:
Code:
<items> 
	<item attr1="attr1" attr2="attr2" attr3="attr3" attr4="attr4" />
        <item attr1="attr1" attr2="attr2" attr3="attr3" attr4="attr4" />
        <item attr1="attr1" attr2="attr2" attr3="attr3" attr4="attr4" />
        <item attr1="attr1" attr2="attr2" attr3="attr3" attr4="attr4" />
        <item attr1="attr1" attr2="attr2" attr3="attr3" attr4="attr4" />
</items>
So to recap, Flash Builder is generating the custom item renderer code. I didnt write anything in that class. The error thats thrown is saying that the 'data' object is null. Not sure why this is. I should be able to say data[column.dataField] or data.@attr4 to get to my data, but it keeps saying data is null.

Also, I'm using Flash Builder 4.5 with the 4.5.1 sdk. All my components are Spark components, I try to avoid MX as much as possible.

Any thoughts on whats going on or how to fix it? Thanks! :-)
Blkhwks19 is offline   Reply With Quote
Old 04-04-2012, 09:16 PM   #2
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

you are setting your array list with data that does not exist. Also even though you are not showing the error, from what you are saying there's no connection with the error and the ItemRenderer. The fact that the ItemRenderer can't find the data doesn't mean it's a problem with the ItemRenderer. You have it backward here.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 04-05-2012, 03:48 PM   #3
Blkhwks19
Member
 
Join Date: Oct 2006
Posts: 85
Default

Ok that makes sense. So it seems the itemrenderer is trying to access the data before its been fully retrieved and is available, thus the reason Im getting the null error.

If thats the case, then how do i make the itemrenderer wait until the data is available before it does its thing? is there a way to "activate" the itemrenderer once the dataprovider is set, or something like that?
Blkhwks19 is offline   Reply With Quote
Old 04-13-2012, 04:15 PM   #4
Blkhwks19
Member
 
Join Date: Oct 2006
Posts: 85
Default

Figured out how to set the itemrenderer in AS, but any way to do it in MXML?
Blkhwks19 is offline   Reply With Quote
Old 05-02-2012, 01:49 PM   #5
Blkhwks19
Member
 
Join Date: Oct 2006
Posts: 85
Default

Still curious about this if anyone has any thoughts.....
Blkhwks19 is offline   Reply With Quote
Old 05-02-2012, 06:22 PM   #6
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

About what?
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 05-03-2012, 01:56 PM   #7
Blkhwks19
Member
 
Join Date: Oct 2006
Posts: 85
Default

Well I was able to accomplish what I needed in Actionscript, but I'm curious if there's a way to do the same thing via MXML. Like an attribute on a tag or something like that.
Blkhwks19 is offline   Reply With Quote
Old 05-16-2012, 02:30 PM   #8
Yui
Senior Member
 
Join Date: Jun 2008
Location: Romania
Posts: 162
Default

Quote:
Originally Posted by Blkhwks19 View Post
how do i make the itemrenderer wait until the data is available before it does its thing?
you don't have to wait.. you can check if the data is available:

ActionScript Code:
override public function prepare(hasBeenRecycled:Boolean):void {                 if (data) {                        lblData.text = data[column.dataField]                        }             }

as for inline itemRenderers: http://livedocs.adobe.com/flex/3/htm...enderer_6.html
Yui is offline   Reply With Quote
Old 05-17-2012, 07:34 AM   #9
Syllogism
capt. Obvious
 
Syllogism's Avatar
 
Join Date: Aug 2006
Location: South Africa
Posts: 826
Default

Why don't you rather overwrite set data and set you values there. Also, remember to call the super() too.
__________________
S y l l o g i s m, Cptn Obvious

"I think you'll find my argument is flawless [Citation][Citation][Citation]"
Syllogism is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:54 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.