AndroidプロジェクトでORMLiteを使用していますが、AsyncTaskのデータベースに値を挿入しているため、拡張アクティビティを使用したくありません。
ドキュメントでは、次のように述べています。
「およびその他の基本クラスを拡張したくない場合はOrmLiteBaseActivity
、それらの機能を複製する必要がありOpenHelperManager.getHelper(Context context, Class openHelperClass)
ます。コードの先頭で呼び出し、ヘルパーを保存して必要なだけ使用してから、次のOpenHelperManager.release()
ときに呼び出す必要があります。あなたはそれで終わりです。」
また、私が持っているデータベースヘルパークラスをに追加するように指示されてstrings.xml
います。だから私は何が間違っているのかわかりません。
DataAccess
次のようなデータ層に呼び出されるクラスを使用しています。
public class DataAccess {
private Context context;
private DBHelper dbHelper;
public DataAccess(Context _context) {
this.context = _context;
dbHelper = getDBHelper(_context);
}
private DBHelper getDBHelper(Context context) {
if (dbHelper == null) {
dbHelper = (DBHelper) OpenHelperManager.getHelper(context, DBHelper.class);
}
return dbHelper;
}
}
そして、私は拡張ヘルパークラスを使用しています:
public class DBHelper extends OrmLiteSqliteOpenHelper {
private static final String DATABASE_NAME = "database.db";
private static final int DATABASE_VERSION = 1;
private Dao<SomeObject, Integer> someObjectTable = null;
private ConnectionSource connectionSource = null;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
this.connectionSource = connectionSource;
try {
TableUtils.createTable(connectionSource, SomeObject.class);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
}
public Dao<SomeObject, Integer> getSomeObjectDao() throws SQLException {
if (someObjectTable == null) {
dateTable = getDao(SomeObject.class);
}
return someObjectTable;
}
アイデアは、DataAccess
クラスを作成し、まだ作成しDBHelper
ていない場合は作成させることです。
誰かがこれが正しいか間違っているか、または私が正しい道を進んでいるかどうかを教えてもらえますか?
ありがとう!