tenía este mismo problema y terminó creando un OrmLiteActivityListener, a escuchar mi RoboActivity y controlar los eventos que plantea.
public class RoboOrmActivityBase<H extends IOrmLiteSqliteOpenHelper> extends RoboActivity {
@Inject RoboOrmLiteActivityListener<H> OrmActivityListener;
/*
* Get a helper for this action.
*/
public H getHelper() {
return OrmActivityListener.getHelper();
}
/**
* Get a connection source for this action.
*/
public ConnectionSource getConnectionSource() {
return OrmActivityListener.getHelper().getConnectionSource();
}
}
clase RoboOrmLiteActivityListener se deriva de la clase OrmLiteActivityBase. Quité su herencia y la hice escuchar en su lugar.
public class RoboOrmLiteActivityListener<H extends IOrmLiteSqliteOpenHelper> {
private volatile H helper;
private volatile boolean created = false;
private volatile boolean destroyed = false;
OrmLiteSqliteOpenHelperProvider databaseHelperProvider;
@Inject
public RoboOrmLiteActivityListener(OrmLiteSqliteOpenHelperProvider databaseHelperProvider) {
this.databaseHelperProvider = databaseHelperProvider;
}
/**
* Get a helper for this action.
*/
public H getHelper() {
if (helper == null) {
if (!created) {
throw new IllegalStateException("A call has not been made to onCreate() yet so the helper is null");
} else if (destroyed) {
throw new IllegalStateException(
"A call to onDestroy has already been made and the helper cannot be used after that point");
} else {
throw new IllegalStateException("Helper is null for some unknown reason");
}
} else {
return helper;
}
}
/**
* Get a connection source for this action.
*/
public ConnectionSource getConnectionSource() {
return getHelper().getConnectionSource();
}
// As you might expect, some events can have parameters. The OnCreate event
// has the savedInstanceState parameter that Android passes to onCreate(Bundle)
public void onCreate(@Observes OnCreateEvent onCreate) {
//Ln.d("onCreate savedInstanceState is %s", onCreate.getSavedInstanceState())
if (helper == null) {
helper = getHelperInternal();
created = true;
}
}
public void onDestroy(@Observes OnDestroyEvent onDestroy) {
releaseHelper(helper);
destroyed = true;
}
/**
* This is called internally by the class to populate the helper object instance. This should not be called directly
* by client code unless you know what you are doing. Use {@link #getHelper()} to get a helper instance. If you are
* managing your own helper creation, override this method to supply this activity with a helper instance.
*
* <p>
* <b> NOTE: </b> If you override this method, you most likely will need to override the
* {@link #releaseHelper(OrmLiteSqliteOpenHelper)} method as well.
* </p>
*/
private H getHelperInternal() {
@SuppressWarnings("unchecked")
H newHelper = (H) databaseHelperProvider.get();
return newHelper;
}
/**
* Release the helper instance created in {@link #getHelperInternal(Context)}. You most likely will not need to call
* this directly since {@link #onDestroy()} does it for you.
*
* <p>
* <b> NOTE: </b> If you override this method, you most likely will need to override the
* {@link #getHelperInternal(Context)} method as well.
* </p>
*/
private void releaseHelper(H helper) {
OpenHelperManager.releaseHelper();
helper = null;
}
}
entonces tengo una DatabaseHelperProvider que proporciona mi ayudante que se extiende desde OrmLiteSqliteOpenHelper. Este proveedor realiza manualmente la inyección en mi asistente después de recuperarlo de OpenHelperManager.
public class OrmLiteSqliteOpenHelperProvider implements Provider<OrmLiteSqliteOpenHelper> {
private final Context context;
private final Injector injector;
@Inject
public OrmLiteSqliteOpenHelperProvider(Context context, Injector injector) {
this.context=context;
this.injector=injector;
}
public OrmLiteSqliteOpenHelper get() {
//The OpenHelperManager built the DatabaseHelper, not us. So we need to guice it up manually.
//Guice normally does its injections when it does its constructions.
OrmLiteSqliteOpenHelper dbhelp = OpenHelperManager.getHelper(context);
injector.injectMembers(dbhelp);
return dbhelp;
}
}
I también tienen una interfaz llamada IOrmLiteSqliteOpenHelper, que contiene miembros extraídos de OrmLiteSqliteOpenHelper. Luego, cuando construyo mi interfaz (IDatabaseHelper) para mi helper que se deriva de OrmLiteSqliteOpenHelper, puedo extender desde IOrmLiteSqliteOpenHelper.
Luego de unirse en mi Módulo tengo
static {
OpenHelperManager.setOpenHelperClass(DatabaseHelper.class);
}
y
@SuppressWarnings("unchecked")
private void bindDataHelperProvider() {
bind(IDatabaseHelper.class)
.toProvider((Class<? extends Provider<? extends IDatabaseHelper>>) OrmLiteSqliteOpenHelperProvider.class);
}
Entonces mi actividad ha de muebles para el RoboGuice y Ormlite manejado enteramente en la clase base y se parece a esto:
public class MyActivity extends RoboOrmActivityBase<IDatabaseHelper> {
List<IMyEntity> lis;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.routine);
try {
Dao<IMyEntity,Integer> myDao = getHelper().getMyDao();
lis = myDao.queryForAll();
Estaría interesado en ayudar @ Sam7, pero no estoy seguro de cómo empezar. Si está preparado, contácteme directamente (http://256.com/gray/email/) con una descripción general y podemos ver si podemos resolverlo. – Gray