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
Developer
Posts: 167
Registered: ‎12-17-2010
My Device: Bold 9000
My Carrier: Vodafone
Accepted Solution

Open database connection in cell renderer?

In my custom cell renderer for a List I want to make use of data from the underlying SQLite database. Based on certain data in the db I want to do different things in my cell renderer. What is the most efficient or best practices way to get data from my SQLite database in the cell renderer?

In my main app class I open the database connection, but this connection is not available in my cell renderer class. So the only thing I can think of is opening another database connection in the cell renderer (which I currently do in the overridden init() function of the cell renderer), but this opens a connection for every row in the List, which is of course not very efficient for the memory use (also because I can't close these connections since you don't know when to close it).

Appreciate your help.

Eugene

My PlayBook App: Checklists
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Open database connection in cell renderer?

You want to separate you storage from your view as much as possible.  I am certain you are doing more with your data than just display it in a list.  What I normally do, I have my database class in a shared class (singleton) that anything can access.  When I want to populate a list (or something), either I fill it when the page comes into view and/or when an event is triggered in response to a state change.  Then fill the data provider for the list.

 

If you are doing "different" things based on the state of the data, place that logic in the renderer.

 

You have to create a container class to hold the data from the database, so you can simply push those containers into the data provider.  I have at times seen that renderers have a hard time with a "class" container vs. a "object" container and it might have to do with the first item not being unique (eg a "data" attribute).  So if you start seeing the renderer acting funny, create a public function to your class to create and return an "Object" representation of your class of information that the renderer cares about. 

Developer
Posts: 167
Registered: ‎12-17-2010
My Device: Bold 9000
My Carrier: Vodafone

Re: Open database connection in cell renderer?

Hi John,

 

To explain what I want to achieve: based on a data record (show in the List as a row and containing a reference to a color record in another table of the db) I want to color the row of the List. How can I achieve that?

(FYI, I am not familiar with Singleton)

Eugene

My PlayBook App: Checklists
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Open database connection in cell renderer?

When setting the data provider of the list, do a lookup to the other table for the actual color value. Since it might be repeated in this case, I would pre-load the colors from the other table so the search/lookup would be faster.

 

Something like: 

mylist.addItem( { data : item.id,  label : item.name, color : mydata.db.colors.Find( item.id ) } ); 

 

Singleton is a "design pattern" like "model-view-controller (MVC)" is a "design pattern". Design Patterns (book by the same name) are coding methods to architect software for greater flexibility and reuse. It helps to reduce the cost to code and maintain software.  It is a convention to give names to certain coding methods.

 

You can do a search of this forum for singleton examples as well as on Google. 

Developer
Posts: 167
Registered: ‎12-17-2010
My Device: Bold 9000
My Carrier: Vodafone

Re: Open database connection in cell renderer?

John, once again, you're great! What I didn't know was that an object that you add to an array that you use to populate the dataprovider of a List with can have any element more than just "label". I thought "label" and "data" where the mandatory ones. So I have extended my query to lookup the colors as well and after that populate the array for the dataprovider with the following statement:

 

myArray.push({label: myName, data: myObject, rowColor:myColor});

 

Thanks again!

Eugene

My PlayBook App: Checklists
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Open database connection in cell renderer?

That is the nice thing about Objects. You can stuff anything into them. They behave like meta data. It is sometimes good to check that an object has a certain attribute before tyring to access it.