Ich verwende ORMLite
Framework auf einem Android-Projekt und ich bin fest, während OpenHelperManager.getHelper()
auf mehrere repo
Klassen.OpenHelperManager: - Setzen neuer Helfer-Klasse bei der Verwendung mehrerer Hilfsklassen in OpenHelperManager ORMLite android
Hier ist eine kurze meiner Code-Struktur: -
DatabaseHelper
Klasse, die für die Erstellung von database
und 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
, die für den Zugriff auf Datenbank verantwortlich ist verantwortlich ist.
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
, die für das Erhalten von Daten aus dem 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;
}
}
eine Serviceklasse ist es funktioniert gut, wenn ich es laufen. Aber wenn ich versuche, eine andere repository
Klasse zuzugreifen, die DatabaseHelper
erstreckt, und verwenden Sie die OpenHelperManager.getHelper()
wie zum Beispiel: -
CategoryRepo categoryRepo = OpenHelperManager.getHelper(activity, CategoryRepo.class);
ich Fehler als 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
Anschaulich bedeutet dies, dass die OpenHelperManager
Instanz von TransactionRepo
gespeichert und wenn ich versuche, es als CategoryRepo
zu bekommen und zu setzen, ist es nicht möglich zu casten oder so etwas.
Wie kann ich dieses Problem lösen?
Danke
Ist da nicht jemand zu rufen, die die Frage beantworten können? Es war Tage und ich bin immer noch fest – viper