Read More

Insert Custom list and get that list in Room database using TypeConverter

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.

What is TypeConverters in Room?

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.

How to create your TypeConverter

Below is the simple Entity class,

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?

adding the Type converters

First of all simply create a type converter class, Named DataConverter

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.



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 :).

. . .

Comments (6)

Add Your Comment

  • Abhijeet Hallur
    Hi Aman Gupta,

    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?

    • Aman Gupta
      Yes, If you want to store OptionValues to the database proper table then you have to create the typeConverter for it.
  • Pabi
    Hey Aman Gupta,
    Awesome article, very helpful.
    • anchit
      Thanks for your appreciation.

      It is good to hear that our team could help you 🙂

  • k pradeep kumar reddy
    so the list field is stored as a jsonString in the DB right ??
    If i want to have a 1 to N relationship tables, then type converters are not the right choice correct ??
    may be then i should use @ForeignKey annotation or some other annotations like @Embedded or @Relation right ??
    • anchit
      Yes as per the article the field is stored as JSONString in the DB.

      Yes, absolutely you need to use different annotations and not the type converters for 1 to N relationship.

  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again