My goal is to add a default value to constructor of the 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.
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).
Android documentation suggests this way to get the string:
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. 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
. As long as I know, Room creates a column for every field in the class. What should I do to provide a language-dependent default value? Thank you!