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; @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; }
android android-room architecture-components
Bohsen
source share