4

I'm working with android and ormlite. I have a problem to initialize a ForeignCollection.

I found this, to initialize it: dao.getEmptyForeignCollection()

But for the dao, i need the DatabaseHelper, and for the databasehelper i need the applicationContext, but in the entity i haven't a context. is there any other option?

And this is the Code, its an 1:n relation. Class Team:

@DatabaseField(canBeNull = true, foreign = true)
private Club club;

Class Club:

@DatabaseField(canBeNull = true, foreign = true)
private ForeignCollection<Team> teams;

Thanks for help

Gray
  • 115,027
  • 24
  • 293
  • 354
Flow
  • 402
  • 6
  • 16

2 Answers2

2

Your question is not about foreign collections but is about how ORMLite gets wired under Android. All of this is explained in the documentation:

http://ormlite.com/docs/android

You specifically asked:

But for the dao, i need the DatabaseHelper, and for the database helper i need the application Context, but in the entity i haven't a context. is there any other option?

Under Android, each class that extends Activity, ListActivity, Service, and TabActivity also is a Context. You can see the class hierarchy from the online docs:

java.lang.Object
    ↳ android.content.Context
        ↳ android.content.ContextWrapper
            ↳ android.view.ContextThemeWrapper
                ↳ android.app.Activity

If you take a look at the Android example projects available from the website you can see how typically an Android application is wired:

  • Your main Activity extends OrmLiteBaseActivity

    public class HelloAndroid extends OrmLiteBaseActivity<DatabaseHelper> {
    
  • The onCreate method in OrmLiteBaseActivity constructs your DatabaseHelper using the ORMLite OpenHelperManager and itself as the Context:

    OpenHelperManager.getHelper(this.getApplicationContext());
    
  • You use the OrmLiteBaseActivity.getHelper() method to get access to the helper to create your DAOs so you can persist things to the database.

Gray
  • 115,027
  • 24
  • 293
  • 354
1

You need to inject the required dependencies.

If you entity needs a database helper, give it. If your database helper needs a context, give it.

Provide constructors to your classes that make the needed dependency compulsory :

public DataHelper( Context context ) {
  this.context = context;
}

Then, from your activity or application model, use

new DataHelper( this );

to inject the dependency.

A priori, it would be interesting for you to use an application class that will provide a general context for your database helper, more than creating a new database helper for each activity in its onCreate method.

Gray
  • 115,027
  • 24
  • 293
  • 354
Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • 1
    Do you mean something like this [link](http://stackoverflow.com/questions/987072/using-application-context-everywhere). Or more something like this 'public Club(Context context, String name) { this.name = name; }' – Flow Apr 11 '12 at 11:05
  • Exactly that :) Happy coding. – Snicolas Apr 11 '12 at 11:06