简体   繁体   English

如何在 Kotlin 中 Room Entity 的构造函数中使用 Android 资源字符串

[英]How to use Android resource string in the constructor of Room Entity in Kotlin

My goal is to add a default value to constructor of the Room @Entity.我的目标是为 Room @Entity 的构造函数添加一个默认值。 The default value must depend on the language settings of the user.默认值必须取决于用户的语言设置。 The way suggested by android framework is to use resource strings. android框架建议的方式是使用资源字符串。

Here's the code I have:这是我的代码:

@Entity
data class ArmyEntity(
    @PrimaryKey(autoGenerate = true)
    val armyId: Long,
    val userOwnerId: Long,
    val name: String = R.string.untitled, // wrong type
    val description: String = R.string.no_description_yet, // wrong type
    val iconUri: String = "",
    val lastEdit: Timestamp = Timestamp(System.currentTimeMillis())
)

The two lines which interest me are labelled with the "wrong type" comments.我感兴趣的两行标有“错误类型”的注释。 Calling R.string.resource_string_name returns resource id, rather than the content of resource (returns Int , not String ).调用R.string.resource_string_name返回资源 ID,而不是资源的内容(返回Int ,而不是String )。

Android documentation suggests this way to get the string: Android 文档建议通过这种方式获取字符串:

val string: String = getString(R.string.hello)

But the issue is that the getString() function is a member of the Context class and can be used in Activity.但问题是getString()函数是Context类的成员,可以在 Activity 中使用。 But Room Entity annotated doesn't know about context.但是带注释的房间实体不知道上下文。 ( Context page for reference ) 上下文页面供参考

I tried passing context as a constructor parameter, but unfortunately, every constructor parameter in the data class has to be val or var .我尝试将上下文作为构造函数参数传递,但不幸的是,数据类中的每个构造函数参数都必须是valvar As long as I know, Room creates a column for every field in the class.据我所知,Room 会为班级中的每个字段创建一列。 What should I do to provide a language-dependent default value?我应该怎么做才能提供依赖于语言的默认值? Thank you!谢谢!

FOR LOCAL RESOURCES本地资源

Define context in your Application class在 Application 类中定义context

class MyApplication : Application() {

    companion object {
        lateinit var context: Context
            private set
    }

    override fun onCreate() {
        super.onCreate()
        context = this
    }
}

and then just use MyApplication.context where you need然后在需要的地方使用MyApplication.context

import com.example.myapp.R
import com.example.myapp.MyApplication

@Entity
data class ArmyEntity(
    @PrimaryKey(autoGenerate = true)
    val armyId: Long,
    val userOwnerId: Long,
    val name: String = MyApplication.context.getString(R.string.untitled),
    val description: String = MyApplication.context.getString(R.string.no_description_yet)
    val iconUri: String = "",
    val lastEdit: Timestamp = Timestamp(System.currentTimeMillis())
)

NOTE : Android studio will warn you about a memory leak.注意:Android Studio 会警告您内存泄漏。 Check this question for more informations检查此问题以获取更多信息

FOR SYSTEM RESOURCES对于系统资源

You can import Resources to access an application's resources with Resources.getSystem() , and the R class.您可以使用Resources.getSystem()R类导入资源以访问应用程序的资源。

import android.content.res.Resources
import com.example.yourapp.R

@Entity
data class ArmyEntity(
    @PrimaryKey(autoGenerate = true)
    val armyId: Long,
    val userOwnerId: Long,
    val name: String = Resources.getSystem().getString(R.string.untitled),
    val description: String = Resources.getSystem().getString(R.string.no_description_yet)
    val iconUri: String = "",
    val lastEdit: Timestamp = Timestamp(System.currentTimeMillis())
)

In my opinion you should use the id of that String.在我看来,您应该使用该字符串的 id。 Change your DB texture a bit to follow this.稍微更改您的数据库纹理以遵循这一点。

  @Entity
data class ArmyEntity(
    @PrimaryKey(autoGenerate = true)
    val armyId: Long,
    val userOwnerId: Long,
    val descriptionRes: Int = R.string.abc, // this is true for multi-languages
    val iconUri: String = "",
)

fun getDescription(context: Context,armyEntity : ArmyEntity,textView: TextView){
    textView.text = context.getString(armyEntity.descriptionRes)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM