我遇到了TypeConverters的问题,我想用它来将整数列表存储到我的房间数据库中。但我不断遇到问题“无法找到方法'值'”,“错误:元素值必须是常量表达式”,“错误:注释类型不适用于此类声明”这是代码。
@Entity
public class Movie {
@PrimaryKey
@SerializedName("id")
private int id;
@ColumnInfo(name = "poster_path")
@SerializedName("poster_path")
private String posterPath;
@ColumnInfo(name = "overview")
@SerializedName("overview")
private String overview;
@ColumnInfo(name = "original_title")
@SerializedName("original_title")
private String title;
@ColumnInfo(name = "release_date")
@SerializedName("release_date")
private String releasedDate;
@ColumnInfo(name = "vote_average")
@SerializedName("vote_average")
private double voteAverage;
@ColumnInfo(name = "genre_ids")
@SerializedName("genre_ids")
private List<Integer> genreIds = null;
public Movie() {
}
public List<Integer> getGenreIds() {
return genreIds;
}
public void setGenreIds(List<Integer> genreIds) {
this.genreIds = genreIds;
}
转换器
public class Converters {
@TypeConverter
public List<Integer> gettingListFromString(String genreIds) {
List<Integer> list = new ArrayList<>();
String[] array = genreIds.split(",");
for(String s: array){
list.add(Integer.parseInt(s));
}
return list;
}
@TypeConverter
public String writingStringFromList(List<Integer> list) {
String genreIds = "";
for(int i : list){
genreIds += ","+i;
}
return genreIds;
}
}
和db
@Database(entities = {Movie.class, Genre.class}, version = 4)
@TypeConverter({Converters.class})
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase instance;
public static AppDatabase getInstance(Context context){
if(instance == null) instance = Room.databaseBuilder(context, AppDatabase.class, "movies_db")
.build();
return instance;
}
public abstract MovieDao movieDao();
public abstract GenreDao genreDao();
}
答案 0 :(得分:9)
好的,最后我找到了解决方案。 而不是
@TypeConverter({Converters.class})
我需要写
@TypeConverters({Converters.class})
错字。