Question

I'm using a db4oHelper in the main activity without problem but when i want use de db in a class without context I've problems.. This class doesn't extends of Activity..

public void actualizatrat(Context context){
    dbHelper();
    db4oHelper.deleteAll();
    //...
}

private Db4oHelper dbHelper() {
if (db4oHelper == null) {
        db4oHelper = new Db4oHelper(this);
        db4oHelper.db();
    }
    return db4oHelper;
}

the constructor db4oHelper:

    public Db4oHelper(Context ctx)
    {
          context = ctx;
    }

Eclipse shows the error: The constructor Db4oHelper(Actualiza) is undefined

can someone please help me?

Was it helpful?

Solution

While the class you want to have access to the helper may not be a Context itself, you likely created it from one of the above components that does, so just pass the Context you have forward.

public void actualizatrat(Context context){
    dbHelper(context);
    db4oHelper.deleteAll();
    (...)
}

private Db4oHelper dbHelper(Context context) {
    if (db4oHelper == null) {
        db4oHelper = new Db4oHelper(context);
        db4oHelper.db();
    }
    return db4oHelper;
}

OTHER TIPS

I suggest getting the Application context instead. Have a look here:

Static way to get 'Context' on Android?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top