Marcos Placona Blog

Programming, technology and the taming of the web.

Category: Adobe AIR (page 1 of 2)

Comments about Adobe AIR, Adobe AIR 2.0 and Adobe AIR tutorials

E4X and XML with namespaces

Reading time: 2 – 3 minutes

Here’s is something that got me scratching my head for a little while today while working on my new mobile application.

In my new application, I’ll be reading XML off various different providers, so I have an interface that gets implemented in various classes to make sure they all obey a certain contract, and I don’t need worry about what type they are (more on that later…)

With that in mind, I ended up implementing different logic on different classes since the XML returned will vary from provider to provider. I am using E4X to get the various information I need from the XML returned, and one of them would not work at all when selected.

I will put two XML examples here, and let you spot the difference:

var basket:XML =

And that should return “Apples”.

Nothing new here, now for the second example:

var basket:XML =
	<foods xmlns="">

At a first glance, I have to admit I was expecting apples as well, but to my surprise, I got… nothing…

It took me a while to to go look on the XML again, and then it hit me. This provider would give me XML with a namespace, and in order to read that with E4X, I would need to declare that namespace. There is absolutely nothing wrong with the code above, but it simply won’t find anything since you’re not declaring what the namespace is, and the compiler gets completely lost.

What you need to do is declare the namespace on the top of my code like so:

namespace items = "";
use namespace items;

How you name it doesn’t matter, but it’s important that it’s unique, so in case you’re reading from multiple XML files on the same class, you should be using different names, otherwise you will get compiling errors.

Background colour in a label control in AS3

Reading time: 1 – 2 minutes

This is just a quick example of how to add a background colour to a label control in AS3.

Most people don’t know, but the label component has a TextField inside of it. What that means is that you can basically use all of the available methods within the TextField in a label. Here’s an example of how to add a background colour to a label.

import fl.controls.Label;
// If you were doing Playbook development
// import qnx.ui.text.Label;
var lblName:Label = new Label();
lblName.textField.background = true;
lblName.textField.backgroundColor = 0xFFFFFF;

And that’s it. You now have a label with a white background. Seems pretty simple, but what is cool here, is the fact that your label can have pretty much the same behaviour as a text field, and you can modify it as you would in a text field.

SQLite databases and mobile applications – A caveat

Reading time: 2 – 4 minutes

As some of you might have noticed, I have been building some mobile applications lately on my spare time specifically for the Blackberry Playbook. They are mainly built in Adobe Air using Actionscript 3 and Blackberry’s Tablet SDK.

On my latest application, I have found the need for a database, as it needs to store lots of user input data. The first option that comes to mind is SQLite, as it’s very simple to integrate, and has native support.

I will show here an integration example, and focus on a caveat I found while trying to use it on a real device.

For starters, here are the classes you need to import on your main class:

import flash.filesystem.File;

I then create a method to initialize my database connection:

private var dbConnection:SQLConnection = new SQLConnection;
private function initDB():void{
	var embededSessionDB:File = File.applicationDirectory.resolvePath("assets/db.sqlite");
	var writeSessionDB:File = File.applicationStorageDirectory.resolvePath("assets/db.sqlite");
	// If a writable DB doesn't exist, we then copy it into the app folder so it's writteable
	if (!writeSessionDB.exists) {
	var dbFile:File = writeSessionDB;;

What is important to mention here, is that most people (myself being one of them) will be completely tempted to skip the bit where it copies the local file (coming from the application itself) into the local storage. Funnily enough, if you skip this step, when you test it locally, it will all work wonderfully, but when you deploy it into a real device (or even a simulator) nothing happens at all.

After some research, I found out that you need to be using the database from the local storage, and just like this is that you will have read and write permissions.

After getting that small “detour” out of the way, I can go on and write my first query against that database.

private function getWords(intTabID:int):Array{
	var stmt:SQLStatement = new SQLStatement();
	stmt.sqlConnection = dbConnection;
	stmt.text = "SELECT key, word FROM WORDS WHERE category_id = (:wordID)";
	stmt.parameters[':wordID'] = intTabID;
	var result:Array = stmt.getResult().data;
	return result;

And this will return a nice array with all my data from the SQLite database.

Quick tip on Flash Builder and application internationalization

Reading time: 1 – 2 minutes

I’m building a new Blackberry Playbook app, and when starting to work with internationalization, I got stuck with a bug that wouldn’t go away. I’m pretty sure this won’t just affect just this kind of application, but anything that is built using Flash Builder and requires resource bundle files.

It’s a pretty common standard to name resource bundle files as *.properties, but it turns out Flash Builder ignores *.properties files when packaging, and you end up with an error like:

It actually took me quite a long time to figure that one out, since I was pretty sure the files were in the correct location. Changing the file extension to .prop did the trick, and now Flash Builder will package them successfully.

I believe there must be another way in Flash builder settings to make it not ignore *.properties files, but for the time being, simply renaming its extension will do.

Hope that helps someone in the future searching for this error.

Date Formatting with Flex

Reading time: 2 – 3 minutes

Formatting dates with flex
(Photo: adactio)

I’ve been working with some Adobe Flex recently, and was having some trouble formatting dates. For those who don’t know, here (in th UK) we use the format “dd/mm/yyyy” (Day/Month/Year) as opposed to people over in the States using “mm/dd/yyyy” (Month/Day/Year).
I had to use the DateField component, and couldn’t find a way in heaven to get the date it returns formatted as “dd/mm/yyyy”.
I consider myself a flex beginner, and haven’t really done much with it (I know, shame on me), and usually have research for online resources (A.K.A “Google Desperation”).
Surprisingly, it’s not very easy to find good online resources for Flex, and when searching for it, I was mostly presented with dodgy date strings being split up and used as arrays.
Damn it! Isn’t there anything like Coldfusion’s DateFormat?
I then had to resource to twitter, and quickly got the answer as if in a water stream!
Basically what you have to to, is use mx:DateFormatter, and create your mask as such:

<mx:DateFormatter id="formatDate" formatString="DD/MM/YYYY" />

You can now use this formatter in order to format any date you might have as a string:

     public var myDate:Date = new Date;

And this will format your date as “dd/mm/yyyy”. I hope this is useful to somebody else some other time.

Older posts