Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
Updated 29 June 2019
We have now that Google announced a set of new libraries for designing Android application’s architecture — Android Architecture Components. And the Room persistent library is one of them.
Before reading this blog you have to sound knowledge of room persistent library, Please read the below blog first,
Android Room Persistence Library
Now we can start.
When you want to store some custom types objects like the e.g lists, models, list of models etc, in the database, you can use Type Converters. It converts your custom object type into a known type in terms of database types. This is most useful features of Room persistent library.
Below is the simple Entity class,
@Entity public class Options extends BaseObservable implements Serializable { @ColumnInfo(name = "option_name") private String optionName; @ColumnInfo(name = "option_values") private List<OptionValues> optionValues; // custom type object @Bindable public String getOptionName() { if (optionName == null) return ""; return optionName; } public void setOptionName(String optionName) { this.optionName = optionName; notifyPropertyChanged(BR.optionName); } public List<OptionValues> getOptionValues() { if (optionValues == null) { optionValues = new ArrayList<>(); return optionValues; } return optionValues; } public void setOptionValues(List<OptionValues> optionValues) { this.optionValues = optionValues; } } 123456789101112131415161718192021222324252627282930313233 @Entitypublic class Options extends BaseObservable implements Serializable { @ColumnInfo(name = "option_name") private String optionName; @ColumnInfo(name = "option_values") private List<OptionValues> optionValues; // custom type object @Bindable public String getOptionName() { if (optionName == null) return ""; return optionName; } public void setOptionName(String optionName) { this.optionName = optionName; notifyPropertyChanged(BR.optionName); } public List<OptionValues> getOptionValues() { if (optionValues == null) { optionValues = new ArrayList<>(); return optionValues; } return optionValues; } public void setOptionValues(List<OptionValues> optionValues) { this.optionValues = optionValues; }}
We can easily see that the option_values column is the list of OptionValues model. So if you run the project it will show some error,
So the question is how to insert the custom types in DB, and your answer is type converters, but how to add?
First of all simply create a type converter class, Named DataConverter
public class DataConverter implements Serializable { @TypeConverter // note this annotation public String fromOptionValuesList(List<OptionValues> optionValues) { if (optionValues == null) { return (null); } Gson gson = new Gson(); Type type = new TypeToken<List<OptionValues>>() { }.getType(); String json = gson.toJson(optionValues, type); return json; } @TypeConverter // note this annotation public List<OptionValues> toOptionValuesList(String optionValuesString) { if (optionValuesString == null) { return (null); } Gson gson = new Gson(); Type type = new TypeToken<List<OptionValues>>() { }.getType(); List<OptionValues> productCategoriesList = gson.fromJson(optionValuesString, type); return productCategoriesList; } } 123456789101112131415161718192021222324252627 public class DataConverter implements Serializable { @TypeConverter // note this annotation public String fromOptionValuesList(List<OptionValues> optionValues) { if (optionValues == null) { return (null); } Gson gson = new Gson(); Type type = new TypeToken<List<OptionValues>>() { }.getType(); String json = gson.toJson(optionValues, type); return json; } @TypeConverter // note this annotation public List<OptionValues> toOptionValuesList(String optionValuesString) { if (optionValuesString == null) { return (null); } Gson gson = new Gson(); Type type = new TypeToken<List<OptionValues>>() { }.getType(); List<OptionValues> productCategoriesList = gson.fromJson(optionValuesString, type); return productCategoriesList; } }
Note we’ve added a new annotation TypeConverter, which allows you to persists a specific custom type into a database. In above class functions(or type converters) simply convert your list of models into Strings and vise-versa.
if you want to use this type converter, you can simply @TypeConverters(DataConverter.class) put in above your objects or add it to your app database class.
E.g,
@Entity public class Options extends BaseObservable implements Serializable { // ... @TypeConverters(DataConverter.class) // add here @ColumnInfo(name = "option_values") private List<OptionValues> optionValues; // .... } 123456789101112 @Entitypublic class Options extends BaseObservable implements Serializable { // ... @TypeConverters(DataConverter.class) // add here @ColumnInfo(name = "option_values") private List<OptionValues> optionValues; // .... } or, @Database(entities = {Options.class, OptionValues.class}, version = 1) @TypeConverters(DataConverter.class) // add here public abstract class AppDatabase extends RoomDatabase { // ... } 1234567 @Database(entities = {Options.class, OptionValues.class}, version = 1)@TypeConverters(DataConverter.class) // add herepublic abstract class AppDatabase extends RoomDatabase { // ... }
or,
Wow now you have successfully added type converters in your app, now you can store the custom type of objects in Database.
Thanks for reading this blog, I hope this blog is helpful to you. If you have any query reading this blog or room database, You can ask in comments.
Stay coollll and updated :).
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Thanks for a neat explanation. I have a question though, what do we need to do if we have List inside the “OptionValues” class? Do we have to add TypeConverters for that list also?
It is good to hear that our team could help you 🙂
But i think, there are toJson() & fromJson() methods in moshi as well and that could realliy help you out for this.
Yes, absolutely you need to use different annotations and not the type converters for 1 to N relationship.
option.getOptionValues() –> this method will give you the list.
If now you want to add any values to this list then you can do the same.
But for passing that list to activity you can again use type converters and convert the list to json string and pass it, then in other activity, you can convert this string to list again using type converters.
If still you want to use room as well inthis then you need to get the instance of the list from your model, update the list as per your need, save it in the Db (you need to make sure the updated value is saved) then in other activity get the updated list from the room model.
Hope this helps you.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Name
Email
Subject
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.