How to instantiate a class of type SQLiteOpenHelper in another simple class


I have two classes one of type SQLiteOpenHelper and another normal one

public class dbdata extends SQLiteOpenHelper{}

and another

public class parsers {}

The Parsers class, reads a json, and I want it to read when I save to the sqlite database, but I can not instantiate the base class.

 mDbCliente=new dbdata();

I know I must pass a parameter to the constructor, but I do not know how I would do in this case, the idea is to make it outside an activity.

I hope your commenters

asked by Alldesign Web 26.04.2017 в 22:12

2 answers


In the case that the class is not Activity or Fragment , what you have to do is to receive in the constructor of the class the context, either of the application or of the Activity , this to be able instantiate your database manager:

mDbCliente = new dbdata(mContext);

for example:

public class MyClass {

private Context mContext;

   //Recibe el contexto.
   public MyClass (Context context) {
       mContext = context

  private void miMethod(){
   mDbCliente = new dbdata(mContext);

answered by 26.04.2017 в 22:21

The idea is to make out an activity.

However, in the long run there is a call that comes from an Activity, so your Parser class should receive, either in its constructor or in the call to the specific method that is executing the Context of your Activity.

answered by 26.04.2017 в 22:23