Space Conservation Library - A nested object with a <Video> list, @Embedded does not work.

I have a VideoList object that I want to save using the room library, but when I try to use @Embedded with an open list List = null; it gives me the error below: Error: (23, 24) error: Unable to figure out how to save this field in the database. You might consider adding a type converter to it.

The VideoList class is as follows.

@Entity public class VideoList { @PrimaryKey public String id; public String title; public String viewType; public Integer sortingOrder = null; public String componentSlug; public String endPoint = null; @Embedded public List<Video> list = null; public boolean hidden = false; } Any suggestions? 
+3
java android database sqlite android-database
source share
2 answers

I think Convertor is the best solution for this type of nested list objects.

 public class Converter { public static String strSeparator = "__,__"; @TypeConverter public static String convertListToString(List<Video> video) { Video[] videoArray = new Video[video.size()]; for (int i = 0; i <= video.size()-1; i++) { videoArray[i] = video.get(i); } String str = ""; Gson gson = new Gson(); for (int i = 0; i < videoArray.length; i++) { String jsonString = gson.toJson(videoArray[i]); str = str + jsonString; if (i < videoArray.length - 1) { str = str + strSeparator; } } return str; } @TypeConverter public static List<Video> convertStringToList(String videoString) { String[] videoArray = videoString.split(strSeparator); List<Video> videos = new ArrayList<Video>(); Gson gson = new Gson(); for (int i=0;i<videoArray.length-1;i++){ videos.add(gson.fromJson(videoArray[i] , Video.class)); } return videos; } 

}

+1
source share

In most cases, you cannot use the converter to create a string, since these are complex objects!

In order not to repeat the answer, in another question you can read my answer .

0
source share

All Articles