gpt4 book ai didi

android - 在哪里可以找到初始化 ORMLite 所需的 ApplicationContext?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:06:44 26 4
gpt4 key购买 nike

我正在使用 android 和 ormlite。我在初始化 ForeignCollection 时遇到问题。

我找到这个,初始化它:dao.getEmptyForeignCollection()

但对于 dao,我需要 DatabaseHelper,对于 databasehelper,我需要 applicationContext,但在实体中我没有上下文。还有其他选择吗?

这就是代码,它是一对一的关系。类(class)团队:

@DatabaseField(canBeNull = true, foreign = true)
private Club club;

类(class)俱乐部:

@DatabaseField(canBeNull = true, foreign = true)
private ForeignCollection<Team> teams;

感谢帮助

最佳答案

你的问题不是关于外国收藏,而是关于如何ORMLite在 Android 下连接。所有这些都在文档中进行了解释:

http://ormlite.com/docs/android

您特别要求:

But for the dao, i need the DatabaseHelper, and for the database helper i need the application Context, but in the entity i haven't a context. is there any other option?

在 Android 下,每个扩展 ActivityListActivityServiceTabActivity 的类 是一个上下文。您可以从 online docs 查看类层次结构:

java.lang.Object
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity

如果你看一下 Android example网站上提供的项目,您可以看到 Android 应用程序的连接方式:

  • 您的主要 Activity 扩展 OrmLiteBaseActivity

    public class HelloAndroid extends OrmLiteBaseActivity<DatabaseHelper> {
  • OrmLiteBaseActivity 中的 onCreate 方法使用 ORMLite OpenHelperManager 构建您的 DatabaseHelper 并将其自身作为上下文:

    OpenHelperManager.getHelper(this.getApplicationContext());
  • 您使用 OrmLiteBaseActivity.getHelper() 方法来访问帮助程序以创建您的 DAO,这样您就可以将内容保存到数据库中。

关于android - 在哪里可以找到初始化 ORMLite 所需的 ApplicationContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10104152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com