Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Highlighted
Developer
Posts: 101
Registered: ‎03-18-2011
My Device: Playbook Simulator
My Carrier: AT&T
Accepted Solution

Trouble binding data to DataGrid

Hey guys, I've got a quick question for you. I'm having trouble getting my data to show up in my DataGrid, and I'm probably doing something stupid that's causing the problem.

 

To explain it quickly, I'm pulling data out of my DB from my DBClass file, then returning from the DBClass function as a DataProvider. When I get the DataProvider back, I create an Array, push my DataProvider into it, then bind it to my DataGrid using new ArrayCollection(array). It sounds like a LOT of swapping around of variable types, but I haven't found an easier way to do it. I find it funny that I can't bind my DataGrid's dataprovider to a dataprovider typed variable, but I suspect it's because it's a spark datagrid and a qnx dataprovider. If there's a better way, please let me know cause all of these datatypes are confusing the hell out of me.

 

Anyway, here's the code:

 

DBClass.as:

 

var result:SQLResult = sqlStatement.getResult();
var dp:DataProvider = new DataProvider();
for each(var obj:Object in result.data) {
	if (obj != null) {
		dp.addItem({week:obj["week"], suHours:obj["su_end"], moHours:obj["mo_end"], tuHours:obj["tu_end"], weHours:obj["we_end"], thHours:obj["th_end"], frHours:obj["fr_end"], saHours:obj["sa_end"]});
	}
}
return dp;

 I didn't include the SQL statement or execution because that part is fine. That's just the DP set up.

 

viewHistory.mxml viewActivateHandler:

 

protected function view1_viewActivateHandler(event:FlexEvent):void
{
	var database:DBClass = new DBClass();
				
	var daHistory:Array = new Array();
	daHistory.push(database.getHistory(1));
	
	if(daHistory.length > 0)
		dgHistory.dataProvider = new ArrayCollection(daHistory);
}

 getHistory(1) is returning the DP from the DBClass.as code listed above, the 1 is just a variable to determine what exactly to return, so it's not related.

 

viewHistory.mxml DataGrid:

 

<s:DataGrid id="dgHistory" width="100%">
	<s:columns>
		<s:ArrayList>
			... some custom itemrenderers that take up a lot of space ...
			<s:GridColumn dataField="suHours" headerText="Sun" />
			<s:GridColumn dataField="moHours" headerText="Mon" />
			<s:GridColumn dataField="tuHours" headerText="Tue" />
			<s:GridColumn dataField="weHours" headerText="Wed" />
			<s:GridColumn dataField="thHours" headerText="Thu" />
			<s:GridColumn dataField="frHours" headerText="Fri" />
			<s:GridColumn dataField="saHours" headerText="Sat" />
		</s:ArrayList>
	</s:columns>
</s:DataGrid>

 I took out a couple of custom item renderers in that last snippet just to save space.

 

 

Issue is all of the columns are blank rather than showing the data that is expected in the array. I've done some debugging to prove that the DP returned DOES have records, and daHistory has records before being put into the ArrayCollection and set as the dataprovider, so I'm not sure where I'm losing my data or setting something wrong.

 

Any help would be appreciated. Like I said, all of these data types are a bit confusing considering it's a new language I'm learning as I go. Thanks!

 

 

Developer
Posts: 101
Registered: ‎03-18-2011
My Device: Playbook Simulator
My Carrier: AT&T

Re: Trouble binding data to DataGrid

[ Edited ]

Solved on my own again! I feel like all I have to do is post something on here and I find the answer myself shortly after. It's not like I haven't been stuck on this since yesterday, but regardless, it works.

 

And to answer my question, yes, I was going WAY out of the way to get my data with way too many data types.

 

I simplified the get function in DBClass.as to just return an array:

 

 

var result:Array = sqlStatement.getResult().data;
return result;

then put the Array in the ArrayCollection for the DG and bound it. Took a second to realize that I'd need to swap the datafield's back to the same name as the DB since I wasn't naming them myself like I was in the DataProvider, but once I did it all works now.

 

 

Developer
Posts: 306
Registered: ‎03-01-2011
My Device: Playbook (sim)
My Carrier: Base

Re: Trouble binding data to DataGrid

no matter how you've solved the problem, please mind that the datagrid is said to be very slow. maybe you're running into performance problems with this...

..................................................................................................................................................................................................................

My App: Peg Solitaire

Need a designer? Drop me a line
Developer
Posts: 101
Registered: ‎03-18-2011
My Device: Playbook Simulator
My Carrier: AT&T

Re: Trouble binding data to DataGrid

I notice a little lag switching to the view, but my data grid is simple enough that I don't think that it should be that much of an issue. What would you suggest otherwise? I'm still learning everything and the datagrid is what I found that does what I need, but I'm always up for suggestions for improvement.
Developer
Posts: 306
Registered: ‎03-01-2011
My Device: Playbook (sim)
My Carrier: Base

Re: Trouble binding data to DataGrid

if it's small it won't be a failure to use it. the alternatives would be nested arrays or vectors. I tend to use extremly deep nested objects, but thats just my way of doing it.

..................................................................................................................................................................................................................

My App: Peg Solitaire

Need a designer? Drop me a line
Developer
Posts: 101
Registered: ‎03-18-2011
My Device: Playbook Simulator
My Carrier: AT&T

Re: Trouble binding data to DataGrid

Well, I'll probably explore other options when I get an actual device to test performance. It's a little hard with the simulator, especially since I turned down the RAM setting to 512 MB since my work machine only has 2GB. Thanks for the suggestions though.
New Contributor
Posts: 2
Registered: ‎03-25-2011
My Device: Blackberry Playbook
My Carrier: N/A

Re: Trouble binding data to DataGrid

Just setting the dataProvider reference on the DataGrid is not guaranteed to refresh the view.

 

I don't remember the exact syntax, but you typically have to do something like dataGrid.dataProvider.refresh()

 

Google for "Flex DataGrid dataProvider refresh" and you'll find lots of commentary on this.

 

DataGrid isn't inherently "slow" either.  Sure there are things you can do to make it slow but even if you throw a LOT of data at it, the internal renderers only deal with what is shown on the screen at any given time.  This is done for performance, but can also lead to some goofy side effects that you have to be aware of if you write your own custom renderers.  The general rule of thumb is: if you build your own renderer -- you have to make it stateless or else you'll get artifacts in the grid you don't want when scrolling.