Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SQLiteOpenHelper without or less restrictive use of Context?

If you extend SQLiteOpenHelper, for the Constructor you have to use a Context. I am wondering if there is a way to leave this out, and be able to work with database tables without a Context.

Or at least be least restrictive, I mean a way of project/class structure that will make history the several context passings I have to do now.

As it is my application has several level of classes, chained in each other, and there are a few that connects to the database, but have no whatsoever influence on the interface, so they don't actually need the Context.

Are you creating your classes in the way that you pass each time a Context to them?
If not, how you do, how you reuse a Context in a short class?

like image 586
Pentium10 Avatar asked May 21 '10 14:05

Pentium10


1 Answers

I am wondering if there is a way to leave this out, and be able to work with database tables without a Context.

Call openDatabase() on SQLiteDatabase.

As it is my application has several level of classes, chained in each other

Please remember that this is a mobile phone you are developing for. Too many abstraction layers will hamper performance.

Are you creating your classes in the way that you pass each time a Context to them?

Sometimes yes, sometimes no.

If not, how you do, how you reuse a Context in a short class?

Use inner classes.

like image 82
CommonsWare Avatar answered Nov 14 '22 22:11

CommonsWare