当在多个活动中访问 ormlite 存储库时,应该有多个实例、单例还是什么?
我正在使用 James Morgan 的 DemoORMLiteAndroid 提供的示例,该示例有一个实例化存储库的活动。
供参考
public class Repository {
private Dao<Room, Integer> roomDao;
public Repository(final DatabaseHelper databaseHelper) {
this.roomDao = getRoomDao(databaseHelper);
...
和活动中
public class RoomActivity extends OrmLiteBaseListActivity<DatabaseHelper> {
private Repository repository;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.repository = new Repository(getHelper());
}
...
this.repository.clearData();
...etc..
应该如何在其他活动或类中访问存储库?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这是一个很好的答案@Kevin,但就这样了。
ORMLite 有几个基类可以帮助引导 Android 数据库。
OrmLiteBaseActivity
OrmLiteBaseActivityGroup
OrmLiteBaseListActivity
OrmLiteBaseService
OrmLiteBaseTabActivity
以下是它们的 Javadocs:http://ormlite.com/javadoc/ormlite-android/
所有这些基类所做的都是提供实用方法,帮助管理扩展 DatabaseHelper 类href="http://ormlite.com/javadoc/ormlite-android/com/j256/ormlite/android/apptools/OrmLiteSqliteOpenHelper.html" rel="nofollow">
OrmLiteSqliteOpenHelper
。您只需要帮助程序类的一个实例,因为它管理与数据库的连接,该连接通过onCreate()
方法。onCreate()
方法是传递与应用程序关联的 AndroidSQLiteDatabase
的方法,ORMLite 需要该方法来包装其数据库连接代码。如果您更具体地询问您想要实现的目标,我将编辑我的回复以包含更多信息。
I'm not sure this a great answer @Kevin but here it goes.
ORMLite has a couple of base classes which help with the bootstrapping of the Android databases.
OrmLiteBaseActivity
OrmLiteBaseActivityGroup
OrmLiteBaseListActivity
OrmLiteBaseService
OrmLiteBaseTabActivity
Here are the Javadocs for them: http://ormlite.com/javadoc/ormlite-android/
All these base classes do is provide utility methods which help manage a
DatabaseHelper
class which extendsOrmLiteSqliteOpenHelper
. You only want one instance of the helper class since it manages the connection to the database which gets passed in with theonCreate()
method.The
onCreate()
method is what gets passed the AndroidSQLiteDatabase
associated with the application which is needed for ORMLite to wrap inside its database connection code.If you ask more specifically what you are trying to accomplish I'll edit my response to include more information.