This is a common problem that I have seen since the announcement of Room. Room does not support the ability to store lists directly, as well as the ability to convert to / from lists. It supports POJO conversion and storage.
In this case, the solution is simple. Instead of saving the List<CountryLang> you want to save the CountryLangs (note the 's')
I made a quick example solution here:
public class CountryLangs { private List<String> countryLangs; public CountryLangs(List<String> countryLangs) { this.countryLangs = countryLangs; } public List<String> getCountryLangs() { return countryLangs; } public void setCountryLangs(List<String> countryLangs) { this.countryLangs = countryLangs; } }
This POJO is the inverse of your previous object. This is an object that stores a list of languages. Instead of a list of objects that store your language.
public class LanguageConverter { @TypeConverter public CountryLangs storedStringToLanguages(String value) { List<String> langs = Arrays.asList(value.split("\\s*,\\s*")); return new CountryLangs(langs); } @TypeConverter public String languagesToStoredString(CountryLangs cl) { String value = ""; for (String lang :cl.getCountryLangs()) value += lang + ","; return value; } }
This converter takes a list of rows and converts them to a comma-separated string, which will be stored in one column. When it extracts a string from SQLite db to convert back, it splits the list into commas and populates CountryLangs.
Forget updating the version of RoomDatabase after making these changes. You have the rest of the configuration correct. A happy hunt with the rest of your work perseverance in the room.
Jack dalton
source share