我在一個android項目上使用ORMLite
框架,我在多個repo
類上使用OpenHelperManager.getHelper()
時卡住了。OpenHelperManager: - 在OpenHelperManager中使用多個幫助器類時設置新的幫助器類ORMLite android
這裏是我的代碼結構的簡單: -
DatabaseHelper
類是負責創建database
和tables
public class DatabaseHelper extends OrmLiteSqliteOpenHelper{
public static String DATABASE_NAME = "muffet.db";
public static int DATABASE_VERSION = 5;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
}
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
// Creating tables
try {
TableUtils.createTable(connectionSource, Category.class);
TableUtils.createTable(connectionSource, Transaction.class);
TableUtils.createTable(connectionSource, Budget.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
TableUtils.dropTable(connectionSource, Category.class, true);
TableUtils.dropTable(connectionSource, Transaction.class, true);
TableUtils.dropTable(connectionSource, Budget.class, true);
onCreate(database, connectionSource);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
TransactionRepo
負責訪問數據庫。
public class TransactionRepo extends DatabaseHelper {
// private Dao<Transaction, Integer> transactionDao = null;
private RuntimeExceptionDao<Transaction, Integer> transactionRuntimeDao = null;
public TransactionRepo(Context context) {
super(context);
}
public RuntimeExceptionDao<Transaction, Integer> getTransactionRuntimeDao(){
if(transactionRuntimeDao == null){
transactionRuntimeDao = getRuntimeExceptionDao(Transaction.class);
}
return transactionRuntimeDao;
}
/**
* Save Transaction to database
*/
public void save(Transaction transaction){
try {
transactionRuntimeDao = getTransactionRuntimeDao();
transactionRuntimeDao.createOrUpdate(transaction);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* Get all list of data
*/
public List<Transaction> getAll(){
try{
transactionRuntimeDao = getTransactionRuntimeDao();
return transactionRuntimeDao.queryForAll();
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
TransactionServiceImpl
這是從TransactionRepo
public class TransactionServiceImpl implements TransactionService {
TransactionRepo transactionRepo;
@Override
public void save(Transaction transaction, Activity activity) {
transactionRepo = OpenHelperManager.getHelper(activity, TransactionRepo.class);
transactionRepo.save(transaction);
OpenHelperManager.releaseHelper();
}
@Override
public List<Transaction> getAll(Activity activity) {
transactionRepo = OpenHelperManager.getHelper(activity, TransactionRepo.class);
List<Transaction> transactionList = transactionRepo.getAll();
OpenHelperManager.releaseHelper();
return transactionList;
}
}
獲取數據的服務類,當我運行它,它工作正常。但是,當我嘗試訪問一些其他repository
類擴展DatabaseHelper
,並使用OpenHelperManager.getHelper()
例如像: -
CategoryRepo categoryRepo = OpenHelperManager.getHelper(activity, CategoryRepo.class);
我得到錯誤的java.lang.IllegalStateException: Helper class was class com.muffet.persistence.repository.TransactionRepo but is trying to be reset to class com.muffet.persistence.repository.CategoryRepo
這顯然意味着,OpenHelperManager
節省的TransactionRepo
實例當我嘗試獲取並設置爲CategoryRepo
時,它無法投射或類似的東西。
我怎樣才能解決這個問題呢?
謝謝
是不是有任何人誰可以回答這個問題
OpenHelperManager.setOpenHelperClass(CategoryRepo.class);
過嗎?這是幾天,我仍然堅持 – viper