1

I'm following the Android developers tutorial to set up a database but I'm having an issue. This is my schema file :

public final class ContactsDBSchema {

    public ContactsDBSchema(){};

    public static abstract class ContactsEntry implements BaseColumns
    {
        public static final String TABLE_NAME = "contacts";
        public static final String COLUMN_CONTACT__ID = "contact_id";
        public static final String COLUMN_CONTACT__NAME = "contact_name";
        public static final String COLUMN_CONTACT__PHONE = "contact_phone";

    }

    public class ContactsDBhelper extends SQLiteOpenHelper
    {
        public static final int DATABASE_VERSION = 1;
        public static final String DATABASE_NAME = "contacts.db";

        public String SQL_CREATE_ENTRIES = "create table" + ContactsEntry.TABLE_NAME + ContactsEntry.COLUMN_CONTACT__ID +"INTEGER PRIMARY KEY AUTOINCREMENT," + ContactsEntry.COLUMN_CONTACT__NAME + "TEXT," + ContactsEntry.COLUMN_CONTACT__PHONE + "TEX";

        public String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS" + ContactsEntry.TABLE_NAME ;

        public ContactsDBhelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        public void onCreate(SQLiteDatabase db) {
            db.execSQL(SQL_CREATE_ENTRIES);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL(SQL_DELETE_ENTRIES);
            onCreate(db);
        }

        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            onUpgrade(db, oldVersion, newVersion);
        }

    }
}

The issue now is when I'm trying to access it from another class like this one:

public class ContactsDBprocess {

    ContactsDBSchema.ContactsDBhelper contactsHelper = new ContactsDBSchema.ContactsDBhelper(getContext());

}

I'm unable to access the helper class properly. The getContext() function does not work and I get an error:

Cannot resolve method getContext

I tried doing this:

ContactsDBSchema.ContactsDBhelper contactsHelper = new ContactsDBSchema.ContactsDBhelper(this)

ContactsDBSchema.ContactsDBhelper contactsHelper = new ContactsDBSchema.ContactsDBhelper(getApplicationContext())

I still don't have any luck. Any suggestions on how to solve this problem?

Razor
  • 1,778
  • 4
  • 19
  • 36
Jimmy
  • 895
  • 3
  • 12
  • 36
  • In what class are you trying to instantiate `ContactsDBhelper`? You need to pass a valid `Context` as the constructor param. – earthw0rmjim Jul 23 '16 at 03:50
  • Your `ContactDBProcess` class needs to either be defined in a class that has `getContext()` or it needs to extend such class. Or you can pass a valid `context` to its constructor and then use that variable instead of `getContext()`. – Shaishav Jul 23 '16 at 04:21

1 Answers1

0

The easiest way to access to your application context from anywhere in your project would be to define a method inside your Application sub-class like this:

public class MyApplication extends Application{
   private static Context context;

   @Override
   public void onCreate(){

      super.onCreate();

      MyApplication.context = getApplicationContext();


   }

   public static Context getAppContext(){
      return MyApplication.context;
   }
}

NOTE

If you were not already extending the Application class in your project, you can do so but remember to set the name of your application in AndroidManifest to the subclass you have created above (MyApplication).

From here, you we be certain to have access to the context anywhere!

MyApplication.getAppContext();

Good luck and I hope this helps you!

Eenvincible
  • 5,641
  • 2
  • 27
  • 46