Search code examples
sqlitelibgdxsqliteopenhelper

SQLite in Core project - LibGDx


I have a LibGDX project with 4 projects inside: libGDX, Proyecto-android, Proyecto-core and Proyecto-desktop. My problem is that I want to use SQLite to have a database, but I have all my clases in my core project (Proyecto-core), so I can't extend SQLiteOpenHelper. The only class that is inside Proyecto-android is AndroidLauncher.java:

package ort.nico.Proyecto.android;

import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import ort.nico.Proyecto.Proyecto;

public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    initialize(new Proyecto(), config);
}
}

Solution

  • You have to interface your code with plateform specific code.

    1) In your core project, to create an interface with methods like initDB(), getMyObject(), storeMyObject(myObject)... Think to all methods you'll need for interaction with your database.

    2) In your core project call the methods where you need interaction with DB. Start by adding your new interface as an argument to your game constructor. In your case: new Proyecto(myInterfaceImplementation)

    3) For each of your project, implements this new interface. That's where you'll use SQLiteOpenHelper in android, initing the DB in your android implementation of initDB()

    More informations in libGDX wiki: https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code