0

I created a game in Android Studio and now I am trying to insert a score in a database. I created a Script in SQL that creates well the database but now I'm trying to insert information but I don't have a context.

This is my DBAdapter:

public class DBAdapter {

private SQLiteDatabase database;
private ScriptSQL dbHelper;
private String[] allColumns = {ScriptSQL.ID, ScriptSQL.PONTUACAO};

public DBAdapter(Context ctx) {
    dbHelper = new ScriptSQL(ctx);
}

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

public void openRead() throws SQLException {
    database = dbHelper.getReadableDatabase();
}

public void close() {
    dbHelper.close();
}

public Score createScore(String pontuacao) {
    ContentValues values = new ContentValues();
    values.put(dbHelper.PONTUACAO, pontuacao);
    long insertId = database.insert(dbHelper.TABLE_NAME, null, values);
    // To show how to query
    Cursor cursor = database.query(dbHelper.TABLE_NAME, allColumns, dbHelper.ID + " = " + insertId, null, null, null, null);
    cursor.moveToFirst();
    return cursorToScore(cursor);
}

This is the non activity class that I'm trying to get the Context.

World.java

public class World {

static final int WORLD_WIDTH = 10; //largura
static final int WORLD_HEIGHT = 13; //altura
static final int SCORE_INCREMENT = 10; //incremento do score
static final float TICK_INITIAL = 0.8f; //velocidade inicial do jogo
static final float TICK_DECREMENT = 0.2f; //velocidade de decremento

public Dog dog;
public Elements element;
public boolean EndGame = false;
public int score = 0;

static int put_element = 0;
public List<Elements> elements = new ArrayList<Elements>();
Random random = new Random();
float timeTick = 0;
static float tick = TICK_INITIAL;

public World(){
    dog = new Dog();
}

/*...*/

DBAdapter datasource;
private Context context;

public void update_database(){

    datasource = new DBAdapter(context.getApplicationContext());

    try {
        datasource.open();
        String point = Integer.toString(score);
        Score c = datasource.createScore(point);
        datasource.close();

    }
    catch (SQLException ex) { }

}

The problem is on the line:

datasource = new DBAdapter(context.getApplicationContext());

I tried to do it this way too but it gives an error because this is a non activity class.

datasource = new DBAdapter(this);

What can I do?

Jorge Martins
  • 31
  • 1
  • 5

2 Answers2

1

The Context lives inside your application and does not belong to a particular Activity, However you still need to access it....

You can pass in a Context to your non-activity class method

public void update_database(Context yourAppContext){

    datasource = new DBAdapter(yourAppContext);
    //Other Stuff........
}
NSTuttle
  • 3,237
  • 2
  • 17
  • 26
  • I did this way: `private Context context; public World(Context context){ dog = new Dog(); this.context = context; }` But when I update the database in the end of the game the app closes immediately. Do I need to pass the context in every java class in my game? – Jorge Martins Dec 03 '16 at 16:36
  • Pass the context into the `update_database()` method, not the `World` constructor. – NSTuttle Dec 03 '16 at 16:51
0

Pass context in constructor in non activity class. While you initialize non activity class pass context

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
  • I did this way: `private Context context; public World(Context context){ dog = new Dog(); this.context = context; }` But when I update the database in the end of the game the app closes immediately. Do I need to pass the context in every java class in my game? – Jorge Martins Dec 03 '16 at 16:40