Android Architecture Components: Using Enums

Is it possible to use the Enum type as a built-in field in the Entity class with new Android architecture components and a space saving library?

My essence (with built-in enum):

@Entity(tableName = "tasks") public class Task extends SyncEntity { @PrimaryKey(autoGenerate = true) String taskId; String title; /** Status of the given task. * Enumerated Values: 0 (Active), 1 (Inactive), 2 (Completed) */ @Embedded Status status; @TypeConverters(DateConverter.class) Date startDate; @TypeConverters(StatusConverter.class) public enum Status { ACTIVE(0), INACTIVE(1), COMPLETED(2); private int code; Status(int code) { this.code = code; } public int getCode() { return code; } } } 

My TypeConverter:

 public class StatusConverter { @TypeConverter public static Task.Status toStatus(int status) { if (status == ACTIVE.getCode()) { return ACTIVE; } else if (status == INACTIVE.getCode()) { return INACTIVE; } else if (status == COMPLETED.getCode()) { return COMPLETED; } else { throw new IllegalArgumentException("Could not recognize status"); } } @TypeConverter public static Integer toInteger(Task.Status status) { return status.getCode(); } } 

When I compile this, I get the error message Error: (52, 12): Entities and Pojos should have a useful public constructor. You may have an empty constructor or a constructor whose parameters correspond to fields (by name and type). ''

Update 1 My SyncEntity Class:

/ ** * Base class for all object objects that are synchronized. * /

 @Entity public class SyncEntity { @ColumnInfo(name = "created_at") Long createdAt; @ColumnInfo(name = "updated_at") Long updatedAt; } 
+8
android android-room architecture-components
source share
2 answers

I can use enum values โ€‹โ€‹in Room with TypeConverters . There are several parts to your code:

1) . You must declare your Entity fields public or must have public getters / setters. Or you will get the error below:

yourField is not publicly available in YourEntity; cannot access from external package

2) You do not need the @Embedded annotation for your status field. This is for nested objects. More from the documentation.

3) You did not use the @TypeConverters annotation in the right place. In your case, it can be set above the status field. More from the documentation.

4) You must define a constructor for your object or you will get an error below:

Entities and Pojos should have a useful public constructor. You may have an empty constructor or a constructor whose parameters correspond to fields (by name and type).

You can define an empty constructor to skip this error.

5) Use int instead of Integer in your TypeConverter.

Amount below works as expected:

 @Entity(tableName = "tasks") public class Task extends SyncEntity { @PrimaryKey(autoGenerate = true) public String taskId; public String title; /** Status of the given task. * Enumerated Values: 0 (Active), 1 (Inactive), 2 (Completed) */ @TypeConverters(StatusConverter.class) public Status status; @TypeConverters(DateConverter.class) public Date startDate; // empty constructor public Task() { } public enum Status { ACTIVE(0), INACTIVE(1), COMPLETED(2); private int code; Status(int code) { this.code = code; } public int getCode() { return code; } } } 
+13
source share

To simplify:

  public enum Status { ACTIVE, INACTIVE, COMPLETED; } 

then your converters:

 @TypeConverter public static Task.Status toStatus(int ordinal) { return Task.Status.values()[ordinal]; } @TypeConverter public static Integer toOrdinal(Task.Status status) { return status.ordinal(); } 
+1
source share

All Articles