I can use enum values at Room
with TypeConverters
. There are some parts to change at your code:
1) You must declare your Entity’s fields public or they must have public getters/setters. Or you’ll get below error:
yourField is not public in YourEntity; cannot be accessed from
outside package
2) You don’t need the @Embedded
annotation for your status
field. It is for nested objects. More from docs.
3) You didn’t use the @TypeConverters
annotation at the correct place. In your case it can be set above the status
field. More from docs.
4) You must define a constructor for your Entity or you’ll get below error:
Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
You can define an empty constructor to skip this error.
5) Use int instead of Integer in your TypeConverter.
Sum; 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;
}
}
}