0

I need to load data from database in RemoteViewFactory class, I'm using OrmLite. I've this Helper class:

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private static final String DATABASE_NAME = "gfkksa.db";
    private static final int DATABASE_VERSION = 1;


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

    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, Issue.class);
            TableUtils.createTable(connectionSource, IssuePriority.class);
        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Can't create database.");
            throw new RuntimeException(e);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
            int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource, Issue.class, true);
            TableUtils.dropTable(connectionSource, IssuePriority.class, true);
            onCreate(db, connectionSource);
        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Can't drop databases.");
            throw new RuntimeException(e);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

    public HashMap<String, Dao> getDaoFactory() {

        HashMap<String, Dao> hashFactory = new HashMap<String, Dao>();
        try {
            hashFactory.put("issue", getDao(Issue.class));
            hashFactory.put("issuePriority", getDao(IssuePriority.class));
        } catch (java.sql.SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return hashFactory;
    }
}

Everything works ok, in Activity class (extends OrmLiteBaseActivity<DatabaseHelper>), for example i'm getting list of data from DB like this:

ArrayList items = (ArrayList) getHelper().getDaoFactory().get("issue").queryForAll();

But if I do the same at class implementing RemoteViewsService.RemoteViewsFactory (Remote views factory for AppWidget's ListView) app crashes with this exception message:

03-18 16:13:29.244: E/AndroidRuntime(28500): java.lang.RuntimeException: Unable to bind to service cz.testbrana.widget.WidgetService@41e34110 with Intent { dat=intent: cmp=cz.testbrana.ebranasystem/cz.testbrana.widget.WidgetService (has extras) }: java.lang.IllegalStateException: A call has not been made to onCreate() yet so the helper is null

How can I use OrmLite in AppWidget's RemoteViewFactory?

1 Answers1

0

I've found the anwser, everything works great when using methods getHelper() and onDestroy() from oficial documentation (http://ormlite.com/docs/android) instead of extending DBHelper.

private DatabaseHelper databaseHelper = null;

@Override
protected void onDestroy() {
    super.onDestroy();
    if (databaseHelper != null) {
        OpenHelperManager.releaseHelper();
        databaseHelper = null;
    }
}

private DBHelper getHelper() {
    if (databaseHelper == null) {
        databaseHelper =
            OpenHelperManager.getHelper(this, DatabaseHelper.class);
    }
    return databaseHelper;
}
Gray
  • 115,027
  • 24
  • 293
  • 354