我很难将我的实体保存到房间持久性数据库中。据我所知,RPL(RoomPersistenceLibrary)只接受原始类型,因此我需要使用Typeconverters将我的两个字段Calendar mStartDate
,Calendar mEndTime
转换为字符串(两个文件都用于设置开始和结束日期) - 每次创建WeekViewEvent
时的分钟,小时,日,月,年 - 请参阅构造函数。)
所以我创建了一个,但是它仍然有问题,因为Android Studio一遍又一遍地向我显示相同的错误: 无法弄清楚如何将此字段保存到数据库(..) 由于控制台没有显示代码行,我无法跟踪错误。
任何想法我可能做错了什么? 预先感谢您的任何帮助! :)
WeekViewEvent.class:
@Entity
public class WeekViewEvent implements Serializable {
@NonNull
@PrimaryKey
private long mId;
private String mName;
private String mLocation;
private int mColor;
private boolean mAllDay;
private Shader mShader;
private String Identifier;
@TypeConverters({WeekViewEventCONVERTER.class})
private Calendar mStartTime;
@TypeConverters({WeekViewEventCONVERTER.class})
private Calendar mEndTime;
public WeekViewEvent(long id, String name, String location, String identifier, int color, int startYear, int startMonth, int startDay, int startHour, int startMinute, int endYear, int endMonth, int endDay, int endHour, int endMinute) {
this.mId = id;
this.mStartTime = Calendar.getInstance();
this.mStartTime.set(Calendar.YEAR, startYear);
this.mStartTime.set(Calendar.MONTH, startMonth - 1);
this.mStartTime.set(Calendar.DAY_OF_MONTH, startDay);
this.mStartTime.set(Calendar.HOUR_OF_DAY, startHour);
this.mStartTime.set(Calendar.MINUTE, startMinute);
this.mEndTime = Calendar.getInstance();
this.mEndTime.set(Calendar.YEAR, endYear);
this.mEndTime.set(Calendar.MONTH, endMonth - 1);
this.mEndTime.set(Calendar.DAY_OF_MONTH, endDay);
this.mEndTime.set(Calendar.HOUR_OF_DAY, endHour);
this.mEndTime.set(Calendar.MINUTE, endMinute);
this.mName = name;
this.Identifier = identifier;
this.mLocation = location;
this.mColor = color;}
...
}
WeekViewEventCONVERTER.class:
@TypeConverter
public static Calendar fromString (String value){
Type calendarType = new TypeToken<Calendar>() {}.getType();
return new Gson().fromJson(value, calendarType);
}
@TypeConverter
public static String fromCalendar (Calendar mStartTime){
Gson gson = new Gson();
String json = gson.toJson(mStartTime);
return json;
}
}
和WeekViewEventDAO:
@Dao
public interface WeekViewEventDAO {
@Insert(onConflict = OnConflictStrategy.REPLACE)
public long insertWeekViewEvent (WeekViewEvent event);
@Update(onConflict = OnConflictStrategy.REPLACE)
public void updateWeekViewEvent (WeekViewEvent event);
@Delete
public void deleteWeekViewEvent (WeekViewEvent event);
@Query("DELETE FROM WeekViewEvent")
public void deleteAllWeekViewEvents();
@Query("SELECT * FROM WeekViewEvent")
WeekViewEvent getWeekViewEvent();
@Query("SELECT * FROM WeekViewEvent")
public List<WeekViewEvent> getAllWeekVieweventsList();}
答案 0 :(得分:2)
我认为您的问题是此字段没有类型转换器:
private Shader mShader;
尝试添加@Ignore
以将其标记为非持久性。