Android Room Database: How to handle Arraylist in an Entity?
JavaAndroidAndroid RoomJava Problem Overview
I just implemented Room for offline data saving. But in an Entity class, I am getting the following error:
Error:(27, 30) error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
And the class is as following:
@Entity(tableName = "firstPageData")
public class MainActivityData {
@PrimaryKey
private String userId;
@ColumnInfo(name = "item1_id")
private String itemOneId;
@ColumnInfo(name = "item2_id")
private String itemTwoId;
// THIS IS CAUSING THE ERROR... BASICALLY IT ISN'T READING ARRAYS
@ColumnInfo(name = "mylist_array")
private ArrayList<MyListItems> myListItems;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public ArrayList<MyListItems> getMyListItems() {
return myListItems;
}
public void setCheckListItems(ArrayList<MyListItems> myListItems) {
this.myListItems = myListItems;
}
}
So basically I want to save the ArrayList in the database but I was not able to find anything relevant to it. Can you guide me regarding how to save an Array using Room?
NOTE: MyListItems Pojo class contains 2 Strings (as of now)
Thanks in advance.
Java Solutions
Solution 1 - Java
Type Converter are made specifically for that. In your case, you can use code snippet given below to store data in DB.
public class Converters {
@TypeConverter
public static ArrayList<String> fromString(String value) {
Type listType = new TypeToken<ArrayList<String>>() {}.getType();
return new Gson().fromJson(value, listType);
}
@TypeConverter
public static String fromArrayList(ArrayList<String> list) {
Gson gson = new Gson();
String json = gson.toJson(list);
return json;
}
}
And mention this class in your Room DB like this
@Database (entities = {MainActivityData.class},version = 1)
@TypeConverters({Converters.class})
More info here
Solution 2 - Java
Option #1: Have MyListItems
be an @Entity
, as MainActivityData
is. MyListItems
would set up a @ForeignKey
back to MainActivityData
. In this case, though, MainActivityData
cannot have private ArrayList<MyListItems> myListItems
, as in Room, entities do not refer to other entities. A view model or similar POJO construct could have a MainActivityData
and its associated ArrayList<MyListItems>
, though.
Option #2: Set up a pair of @TypeConverter
methods to convert ArrayList<MyListItems>
to and from some basic type (e.g., a String
, such as by using JSON as a storage format). Now, MainActivityData
can have its ArrayList<MyListItems>
directly. However, there will be no separate table for MyListItems
, and so you cannot query on MyListItems
very well.
Solution 3 - Java
Kotlin version for type converter:
class Converters {
@TypeConverter
fun listToJson(value: List<JobWorkHistory>?) = Gson().toJson(value)
@TypeConverter
fun jsonToList(value: String) = Gson().fromJson(value, Array<JobWorkHistory>::class.java).toList()
}
I Used JobWorkHistory
object for my purpose, use the object of your own
@Database(entities = arrayOf(JobDetailFile::class, JobResponse::class), version = 1)
@TypeConverters(Converters::class)
abstract class MyRoomDataBase : RoomDatabase() {
abstract fun attachmentsDao(): AttachmentsDao
}
Solution 4 - Java
Better version of List<String>
converter
class StringListConverter {
@TypeConverter
fun fromString(stringListString: String): List<String> {
return stringListString.split(",").map { it }
}
@TypeConverter
fun toString(stringList: List<String>): String {
return stringList.joinToString(separator = ",")
}
}
Solution 5 - Java
Native Kotlin version using Kotlin's serialization component – kotlinx.serialization.
- Add the Kotlin serialization Gradle plugin and dependency to your
build.gradle
:
apply plugin: 'kotlinx-serialization'
dependencies {
...
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1"
}
- Add the Type converters to your Converter class;
class Converters {
@TypeConverter
fun fromList(value : List<String>) = Json.encodeToString(value)
@TypeConverter
fun toList(value: String) = Json.decodeFromString<List<String>>(value)
}
- Add your Converter class to your database class:
@TypeConverters(Converters::class)
abstract class YourDatabase: RoomDatabase() {...}
And you're done!
Extra resources:
Solution 6 - Java
Kotlin Answer
You need to do 3 things:
- Create Converters class.
- Add Converters class on Database.
- Just define what you want to use in Entity class.
Usage example step by step:
Step 1 :
class Converters {
@TypeConverter
fun listToJsonString(value: List<YourModel>?): String = Gson().toJson(value)
@TypeConverter
fun jsonStringToList(value: String) = Gson().fromJson(value, Array<YourModel>::class.java).toList()
}
Step 2 :
@Database(entities = [YourEntity::class], version = 1)
@TypeConverters(Converters::class)
abstract class YourDatabase : RoomDatabase() {
abstract fun yourDao(): YourDao
}
Step 3 :
> Note: You do not need to call functions of Converter which are listToJsonString() and jsonStringToList(). They are using in background by Room.
@Entity(tableName = "example_database_table")
data class YourEntity(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
@ColumnInfo(name = "your_model_list") var yourModelList: List<YourModel>,
)
Solution 7 - Java
This is how i handle List conversion
public class GenreConverter {
@TypeConverter
public List<Integer> gettingListFromString(String genreIds) {
List<Integer> list = new ArrayList<>();
String[] array = genreIds.split(",");
for (String s : array) {
if (!s.isEmpty()) {
list.add(Integer.parseInt(s));
}
}
return list;
}
@TypeConverter
public String writingStringFromList(List<Integer> list) {
String genreIds = "";
for (int i : list) {
genreIds += "," + i;
}
return genreIds;
}}
And then on the database i do as shown below
@Database(entities = {MovieEntry.class}, version = 1)
@TypeConverters(GenreConverter.class)
And below is a kotlin implementation of the same;
class GenreConverter {
@TypeConverter
fun gettingListFromString(genreIds: String): List<Int> {
val list = mutableListOf<Int>()
val array = genreIds.split(",".toRegex()).dropLastWhile {
it.isEmpty()
}.toTypedArray()
for (s in array) {
if (s.isNotEmpty()) {
list.add(s.toInt())
}
}
return list
}
@TypeConverter
fun writingStringFromList(list: List<Int>): String {
var genreIds=""
for (i in list) genreIds += ",$i"
return genreIds
}}
Solution 8 - Java
I would personally advise against @TypeConverters
/serializations, since they break the database's normal forms compliance.
For this particular case it might be worth defining a relationship using the @Relation annotation, which allows to query nested entities into a single object without the added complexity of declaring a @ForeignKey
and writing all the SQL queries manually:
@Entity
public class MainActivityData {
@PrimaryKey
private String userId;
private String itemOneId;
private String itemTwoId;
}
@Entity
public class MyListItem {
@PrimaryKey
public int id;
public String ownerUserId;
public String text;
}
/* This is the class we use to define our relationship,
which will also be used to return our query results.
Note that it is not defined as an @Entity */
public class DataWithItems {
@Embedded public MainActivityData data;
@Relation(
parentColumn = "userId"
entityColumn = "ownerUserId"
)
public List<MyListItem> myListItems;
}
/* This is the DAO interface where we define the queries.
Even though it looks like a single SELECT, Room performs
two, therefore the @Transaction annotation is required */
@Dao
public interface ListItemsDao {
@Transaction
@Query("SELECT * FROM MainActivityData")
public List<DataWithItems> getAllData();
}
Aside from this 1-N example, it is possible to define 1-1 and N-M relationships as well.
Solution 9 - Java
Had the same error message as described above. I would like to add: if you get this error message in a @Query, you should add @TypeConverters above the @Query annotation.
Example:
@TypeConverters(DateConverter.class)
@Query("update myTable set myDate=:myDate where id = :myId")
void updateStats(int myId, Date myDate);
....
public class DateConverter {
@TypeConverter
public static Date toDate(Long timestamp) {
return timestamp == null ? null : new Date(timestamp);
}
@TypeConverter
public static Long toTimestamp(Date date) {
return date == null ? null : date.getTime();
}
}
Solution 10 - Java
This answer uses Kotin to split by comma and construct the comma delineated string. The comma needs to go at the end of all but the last element, so this will handle single element lists as well.
object StringListConverter {
@TypeConverter
@JvmStatic
fun toList(strings: String): List<String> {
val list = mutableListOf<String>()
val array = strings.split(",")
for (s in array) {
list.add(s)
}
return list
}
@TypeConverter
@JvmStatic
fun toString(strings: List<String>): String {
var result = ""
strings.forEachIndexed { index, element ->
result += element
if(index != (strings.size-1)){
result += ","
}
}
return result
}
}
Solution 11 - Java
in my case problem was generic type base on this answer
[https://stackoverflow.com/a/48480257/3675925][1] [1]: https://stackoverflow.com/a/48480257/3675925
use List
import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
class IntArrayListConverter {
@TypeConverter
fun fromString(value: String): List<Int> {
val type = object: TypeToken<List<Int>>() {}.type
return Gson().fromJson(value, type)
}
@TypeConverter
fun fromArrayList(list: List<Int>): String {
val type = object: TypeToken<List<Int>>() {}.type
return Gson().toJson(list, type)
}
}
it doesn't need add @TypeConverters(IntArrayListConverter::class) to query in dao class nor fields in Entity class and just add @TypeConverters(IntArrayListConverter::class) to database class
@Database(entities = [MyEntity::class], version = 1, exportSchema = false)
@TypeConverters(IntArrayListConverter::class)
abstract class MyDatabase : RoomDatabase() {
Solution 12 - Java
When We r using TypaConverters Then Datatype Should be return type of TypeConverter method . Example TypeConverter method Return String then Adding Table COloum should be String
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
// Since we didn't alter the table, there's nothing else to do here.
database.execSQL("ALTER TABLE "+ Collection.TABLE_STATUS + " ADD COLUMN deviceType TEXT;");
database.execSQL("ALTER TABLE "+ Collection.TABLE_STATUS + " ADD COLUMN inboxType TEXT;");
}
};
Solution 13 - Java
All above answers are for list of strings. But below helps you to write converter for list of objects.
Just in place of "YourClassName", add your Object class.
@TypeConverter
public String fromValuesToList(ArrayList<**YourClassName**> value) {
if (value== null) {
return (null);
}
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<**YourClassName**>>() {}.getType();
return gson.toJson(value, type);
}
@TypeConverter
public ArrayList<**YourClassName**> toOptionValuesList(String value) {
if (value== null) {
return (null);
}
Gson gson = new Gson();
Type type = new TypeToken<List<**YourClassName**>>() {
}.getType();
return gson.fromJson(value, type);
}
Solution 14 - Java
Adding @TypeConverters
with the converter class as params
to Database & to the Dao class, made my queries work
Solution 15 - Java
Json conversions don't scale well in terms of memory allocation.I'd rather go for something similar to responses above with some nullability.
class Converters {
@TypeConverter
fun stringAsStringList(strings: String?): List<String> {
val list = mutableListOf<String>()
strings
?.split(",")
?.forEach {
list.add(it)
}
return list
}
@TypeConverter
fun stringListAsString(strings: List<String>?): String {
var result = ""
strings?.forEach { element ->
result += "$element,"
}
return result.removeSuffix(",")
}
}
For simple data types the above can be used, otherwise for complex datatypes Room provides [Embedded][1] [1]: https://developer.android.com/reference/android/arch/persistence/room/Embedded
Solution 16 - Java
Here is the example for adding the customObject types to Room DB table. https://mobikul.com/insert-custom-list-and-get-that-list-in-room-database-using-typeconverter/
Adding a type converter was easy, I just needed a method that could turn the list of objects into a string, and a method that could do the reverse. I used gson for this.
public class Converters {
@TypeConverter
public static String MyListItemListToString(List<MyListitem> list) {
Gson gson = new Gson();
return gson.toJson(list);
}
@TypeConverter
public static List<Integer> stringToMyListItemList(@Nullable String data) {
if (data == null) {
return Collections.emptyList();
}
Type listType = new TypeToken<List<MyListItem>>() {}.getType();
Gson gson = new Gson();
return gson.fromJson(data, listType);
}
}
I then added an annotation to the field in the Entity:
@TypeConverters(Converters.class)
public final ArrayList<MyListItem> myListItems;
Solution 17 - Java
@Query("SELECT * FROM business_table")
abstract List<DatabaseModels.Business> getBusinessInternal();
@Transaction @Query("SELECT * FROM business_table")
public ArrayList<DatabaseModels.Business> getBusiness(){
return new ArrayList<>(getBusinessInternal());
}
Solution 18 - Java
All answers above correct. Yes, if you REALLY need store array of something into one SQLite field TypeConverter is a solution.
And I used the accepted answer in my projects.
But don't do it!!!
If you need store array in Entity in 90% cases you need to create one-to-many or many-to-many relationships.
Otherwise, your next SQL query for select something with key inside this array will be absolutely hell...
Example:
Object foo comes as json: [{id: 1, name: "abs"}, {id:2, name: "cde"}
Object bar: [{id, 1, foos: [1, 2], {...}]
So don't make entity like:
@Entity....
data class bar(
...
val foos: ArrayList<Int>)
Make like next:
@Entity(tablename="bar_foo", primaryKeys=["fooId", "barId"])
data class barFoo(val barId: Int, val fooId: Int)
And sore your foos:[] as records in this table.
Solution 19 - Java
Use official solution from room, @Embedded annotation :
@Embedded(prefix = "mylist_array") private ArrayList<MyListItems> myListItems