当在多个活动中访问 ormlite 存储库时,应该有多个实例、单例还是什么?

发布于 2024-12-02 09:48:35 字数 812 浏览 1 评论 0 原文

我正在使用 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..

应该如何在其他活动或类中访问存储库?

I am using the sample provided by James Morgan's DemoORMLiteAndroid which has one activity that instantiates a repository.

for reference

public class Repository {
    private Dao<Room, Integer> roomDao;

    public Repository(final DatabaseHelper databaseHelper) {
        this.roomDao = getRoomDao(databaseHelper);
        ...

and in Activity

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..

How should the repository be accessed in other activities or classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森罗 2024-12-09 09:48:35

我不确定这是一个很好的答案@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() 方法是传递与应用程序关联的 Android SQLiteDatabase 的方法,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 extends OrmLiteSqliteOpenHelper. You only want one instance of the helper class since it manages the connection to the database which gets passed in with the onCreate() method.

The onCreate() method is what gets passed the Android SQLiteDatabase 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文