Home > Software design >  Room database : How set default Value from strings resources
Room database : How set default Value from strings resources

Time:11-28

How can I set the default value of some fields from resources like strings . because I want set value for different languages

@ColumnInfo(defaultValue = "") // R.string.someword
private String publisher;

When trying to :

@ColumnInfo(defaultValue = Resources.getSystem().getString(android.R.string.someword)) 
private String publisher;

Get the following error

Attribute value must be constant

CodePudding user response:

In short you cannot as such values can change at run time, whilst Room builds the SQL that creates the tables, indexes and views at compile time.

The default = "" parameter is used to apply the value to the DEFAULT construct of the CREATE TABLE .... sql.

If you successfully compile you can see the generated SQL it's in the java generated (Android View) in the file that is the same name as the @Database class but suffixed with _Impl.

  • Related