0

I am creating some hack kind of thing in existing android code to verify database creation and its accessibility across layers in application.

For this I have modified an existing function of .java file but I am facing an issue while calling constructor of SQLiteOpenHelper.

The signature is SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)

And I don't know how to create this Context instance. From googling I am seeing it is being some kind of activity class instance.

What ways are there to create this Context instance? Do we have to have activity class implemented?

Nat Ritmeyer
  • 5,634
  • 8
  • 45
  • 58
Amit
  • 21
  • 4

2 Answers2

1

Have a look at this question. It shows how to obtain a reference to the current Context object statically.

The gist of it is that you have to store a reference to the context that can be accessed statically from other sections of code.

P.S. You can't really "create" a context. That is something that is provided to you by the Android platform.

Community
  • 1
  • 1
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
0

Just call:

this.getApplicationContext()

from wherever you are trying to create the instance of SQLiteOpenHelper.

j0k
  • 22,600
  • 28
  • 79
  • 90