Android Room TypeConverters无法识别

我正在尝试实现DateConverter来保持日期。 这很通用,但我一直在:

无法弄清楚如何将此字段保存到数据库中。 您可以考虑为其添加types转换器。



 compile('') { exclude group: '' } compile('') { exclude group: '' } testCompile('') { exclude group: '' } annotationProcessor('') { exclude group: '' } 


 @Database(entities = {Location.class, Room.class, Amenity.class, UserModel.class, EventModel.class}, version =1, exportSchema = false) @TypeConverters({DateConverter.class}) public abstract class AppDatabase extends RoomDatabase { public abstract RoomDao getRoomDao(); public abstract LocationDao getLocationDao(); public abstract AmenityDao getAmenityDao(); public abstract UserDao getUserDao(); public abstract EventDao getEventDao(); } 


 public class DateConverter { @TypeConverter public static Date toDate(Long timestamp) { return timestamp == null ? null : new Date(timestamp); } @TypeConverter public static long toTimestamp(Date date) { return date == null ? null : date.getTime(); } } 


 @Entity(tableName = "event") @TypeConverters({DateConverter.class}) public class EventModel { @PrimaryKey private String uuid; @ColumnInfo(name = "subject") private String subject; @TypeConverters({DateConverter.class}) private Date start; @TypeConverters({DateConverter.class}) private Date end; @ColumnInfo(name = "confirmed") private Boolean confirmed; public String getUuid() {return uuid;} public void setUuid(String uuid) {this.uuid = uuid;} public String getSubject() {return subject;} public void setSubject(String subject) { this.subject = subject; } public Date getStart() {return start;} public void setStart(Date date) {this.start = date;} public Date getEnd() {return end;} public void setEnd (Date end) { this.end = end; } public Boolean getConfirmed() {return confirmed;} public void setConfirmed(Boolean confirmed) { this.confirmed = confirmed; } } 

我还遗失了什么吗? 谢谢!


 class DateConverter { @TypeConverter fun toDate(timestamp: Long?): Date? { return when (timestamp) { null -> null else -> Date(timestamp) } } @TypeConverter fun toTimestamp(date: Date?): Long? { return date?.time } } 


 @Database(entities = arrayOf(MyObject::class), version = 1) @TypeConverters(DateConverter::class) abstract class MyObjectDb : RoomDatabase() { abstract fun myObjectDao(): MyObjectDao } 


 public static long toTimestamp(Date date) 


 public static Long toTimestamp(Date date) 

或者,将另一个参数更改为long 。 IOW,让他们使用相同的types。

例如,在此示例项目中 ,我使用以下类,它工作正常:

 /*** Copyright (c) 2017 CommonsWare, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Covered in detail in the book _Android's Architecture Components_ */ package; import; import java.util.Date; public class TypeTransmogrifier { @TypeConverter public static Long fromDate(Date date) { if (date==null) { return(null); } return(date.getTime()); } @TypeConverter public static Date toDate(Long millisSinceEpoch) { if (millisSinceEpoch==null) { return(null); } return(new Date(millisSinceEpoch)); } }