Android Room - Error: Cannot Figure Out How To Save This Field Into Database
Solution 1:
Date
is exactly the example given in https://developer.android.com/training/data-storage/room/referencing-data.
For example, if we want to persist instances of Date, we can write the following TypeConverter to store the equivalent Unix timestamp in the database:
publicclassConverters { @TypeConverterpublicstaticDatefromTimestamp(Long value) { return value == null ? null : newDate(value); } @TypeConverterpublicstaticLongdateToTimestamp(Date date) { return date == null ? null : date.getTime(); } }
The preceding example defines 2 functions, one that converts a Date object to a Long object and another that performs the inverse conversion, from Long to Date. Since Room already knows how to persist Long objects, it can use this converter to persist values of type Date.
Next, you add the @TypeConverters annotation to the AppDatabase class so that Room can use the converter that you've defined for each entity and DAO in that AppDatabase:
AppDatabase.java
@Database(entities = {User.class}, version = 1)@TypeConverters({Converters.class})publicabstractclassAppDatabaseextendsRoomDatabase { publicabstract UserDao userDao(); }
A side note: java.util.Date
is considered to be badly designed (and java.util.Calendar
is much worse). If you have any non-trivial date-time logic and can get away with API level 26 (Java 8 on desktop), it's generally better to use java.time
package. And if you can't, see https://github.com/JakeWharton/ThreeTenABP for a backport.
Solution 2:
// Java code will not convert to Kotlin very // well so here is the Kotlin: Converter // classpublicclassConverters{
@TypeConverterfunfromTimestamp( value: Long?) :
java.sql.Date {
return java.sql.Date(value ?: 0)
}
@TypeConverterfundateToTimestamp(date :java.sql.Date?)
:Long {
return date?.getTime() ?: 0
}
// Here is the type converters example in // Kotlin@Database(entities = [DbNasaPictures::class],
version = 2)@TypeConverters(Converters::class)abstractclassPicturesDatabase:
RoomDatabase() {
Solution 3:
All above answers is for list of strings.But below helps you to find converter for list of your objects.
Just in place of "YourClassName" ,add your Object class.
@TypeConverterpublicStringfromValuesToList(ArrayList<**YourClassName**> value) {
if (value== null) {
return (null);
}
Gson gson = newGson();
Typetype = newTypeToken<ArrayList<**YourClassName**>>() {}.getType();
return gson.toJson(value, type);
}
@TypeConverterpublicArrayList<**YourClassName**> toOptionValuesList(String value) {
if (value== null) {
return (null);
}
Gson gson = newGson();
Typetype = newTypeToken<List<**YourClassName**>>() {
}.getType();
return gson.fromJson(value, type);
}
Solution 4:
In Kotlin, Need to add @TypeConverters in Two files
- Database class in which extend by RoomDatabase
@Database(entities = [ToDo::class], version = 1, exportSchema = false)@TypeConverters(DateConverter::class)abstractclassAppDatabase : RoomDatabase() {
abstractfuntodoDao(): ToDoDao
companionobject {
@Volatileprivatevar instance: AppDatabase? = nullfungetDatabase(context: Context): AppDatabase =
instance ?: synchronized(this) { instance ?: buildDatabase(context).also { instance = it } }
privatefunbuildDatabase(appContext: Context) =
Room.databaseBuilder(appContext, AppDatabase::class.java, "todo")
.fallbackToDestructiveMigration()
.build()
}
}
- Data class in which @Entity declared and need to add DateConverter class for Date
@Entity(tableName = "todo")
data class ToDo(
@PrimaryKey
val id: Int,
val title: String,
val description: String,
val time: String,
val date: String,
val types: Int,
@TypeConverters(DateConverter::class)
val date_time: Date,
val created: String
)
DateConverter class
import androidx.room.TypeConverter
import java.util.*
classDateConverter{
@TypeConverterfuntoDate(timestamp: Long): Date {
return Date(timestamp)
}
@TypeConverterfuntoTimestamp(date: Date): Long {
return date.time
}
}
Post a Comment for "Android Room - Error: Cannot Figure Out How To Save This Field Into Database"