ADAFramework: How to work with relationships

In this post I’m going to talk about ADA Framework again, specifically about how to work with relationships. I’ve tried to publish it as soon as it was possible but It tooks me more than I thought because, as you can see, I decided to write it in English😉.

Well! Let’s begin at the biginning!😉


Development envirnomment information:

  • Windows 7 Home Premium 64bits.
  • Eclipse Indigo and ADT Plugin for Eclipse.
  • Android SDk 2.2 (min. sdk 8).
  • ADA Framework 1.4.5.
  • Tested on Samsung Galaxy ACE GT-S5830 con Android 2.3.5
  • SVN repository download: svn://

Project package explorer:

In this case I’ve created an Android project related to music and to song rates. I’ve defined two concepts:

1.- We are going to work with two entities call Song and Album.

2.- One Song is linked to on Album neither one Album is linked to many Songs.

ADA Framework make our work with our relationships easier because we now we only have to give an Album entity to our Song entity. Look at this code:

The first one ir Song class and the second one is Album class and both extend from com.desandroid.framework.ada.Entity like always. As you can see we have defined our relationship using the “album” attribute of Album type and we have defined it as Entity.DATATYPE_ENTITY_REFERENCE. Now we can to start to work with our entities.

Like in previous post, we are going to create an entity manager that allows to work get information from database and to save, to update and to delete (CRUD operationes) data in database. This is ApplicationDataContext and it extends to com.desandroid.framework.ada.ObjectContext. Let’s see a bit of code:

It’s simple, isn’t it?. Look at the first piece of code. it contains two com.desandroid.framework.ada.ObjectSet attributes to manage our two entity types. They allows us to execute all CRUD operation wich ObjectSet class contains. Now, when we create a ApplicationDataContext instance ir our Activities we will have two structures to do INSERT, UPDATE, DELETE and SELECT (basic and complex) operations with Songs and Albums, and when we won’t do anything special if we will go to save a song with a album or a position or a name, that’s the same.

Now we are going to talk about Activity classes in the project. I’ve created two activities.

1.- HomeActivity: Allows to list all songs with this format: POSITION .- SONG NAME (RATING) .

2.- SongDetails: Allows to create a new activity or to vote for songs.

I’m going to talk only about the most important in this classes, but if you have doubts, suggestions or corrections you just write your comment😉.

As we haven’t songs and albums initially, when we run the frrist time it found that database is empty and makes several songs and albums and their relationships. For next times load ones and it doesn’t create again.

* Take a loot at initializedDatabase() method in project source code if you don’t understand what it does.

As you can see, HomeActivity is our ListView component to show songs and it contains an ArrayAdapter to magane songs and to put into the ListView. Too, it have an ApplicationDataContext attribute to get songs from database executing dataContext.getSongSet().fill() line code.

At the end of onCreate method we define the same adapter for song records in database and in ListView. Yes! This seems weird but is the way to define the relationship between database and Activity classes.

What happen when I do click in a lisview item?… Well it shows you detailed information about that item (Song) that allows you to vote for songs.

We have two options, as I said, vote for a song (onListItemClick overrided method) or, if you want to create a new song, press your menu device menu button (onOptionItemSelected overrided method). In the second one you can see that we passing parameters bettween Activities with intent.putExtra(ARGUMENT_NAME,ARGUMENT_VALUE), with SONG_ID. It allows to find this song in database to load it in SongDetails Activity.

One more thing about HomeActivity class! To refresh that ListView when we update it I’ve overrided onResume method, and I’d like to know your opinion about that. Do you know another ways to do that? How do you usually do that?

Now we are going to look at SongDetails Activity.

SongDetails activity detects if we want to create or not to create a song checking if we have passed SONG_ID parameter from HomeActivity.

If we want to create a song, we load albums from database into Spinner component (loadAlbumsSpinner method) and set form field as Editable state:

Press on album spinner:

Else, we search in database the song clicked in HomeActivity ListView and load her detailed information (loadInformation method) :

Finally when we do click in vote button vote method is executed:

And it execute one of two queries using the same method dataContext.getSongsSet().save(SONG_OBJECT):

1.- INSERT INTO ‘tSong’: Put  attention on  song.setAlbum((Album) spinner.getSelecteditem())!! We are creating our relashionship between Song and Album as if only was a simple data type like String, int, boolean, etc. 

2.- UPDATE TABLE ‘tSong’: Put attention on song.setStatus.(Entity.STATUS_UPDATED), because if you don’t change that property ADA Framework won’t do anything and your changes won’t be saved.

Fill the form and press on vote spinner:

Now it’s your turn! Press device menu button whe you are showing any song details😉

My most sincere thanks for @DesAndrOId

Acerca de vicmonmena

Just trying to contribute with my code to make this world easier
Esta entrada fue publicada en Android y etiquetada , , , , . Guarda el enlace permanente.

4 respuestas a ADAFramework: How to work with relationships

  1. Cynthia dijo:

    Hola Vicente… leí tus 3 entradas sobre ADA Framework y me parecieron excelentes … Por favor me gustaría que me puedas enviar el proyecto ejemplo a mi correo, ya que no puedo acceder a través de SVN. Saludos de Perú! Un abrazo

  2. Johng403 dijo:

    Good day! Do you know if they make any plugins to safeguard against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations? gceeadfkcegc

    • vicmonmena dijo:

      Hi John!
      If I’ve understood what you need, you want encrypt your database?
      ADA allows you do this just indicating an special argument when you define database fields.
      Ask again if you need more information about that.

  3. Hi can you please show some code to query database with the help of ada framework.. means code for loadInformation(Songid).


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de

Estás comentando usando tu cuenta de Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s