Android Room - error: Cannot figure out how to save this field into database

前端 未结 3 1592
清酒与你
清酒与你 2020-12-16 19:22

Detailed log

error: Cannot figure out how to save this field into database. You can 
consider adding a type converter for it.
private final java.util.Date m         


        
相关标签:
3条回答
  • 2020-12-16 19:57

    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.

     @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);
            }
    
    0 讨论(0)
  • 2020-12-16 20:05
        // Java code will not convert to Kotlin very 
        // well so here is the Kotlin: Converter 
        // class
    
        public class Converters {
            @TypeConverter
            fun fromTimestamp( value: Long?) : 
                           java.sql.Date {
                return java.sql.Date(value ?: 0)
            }
            @TypeConverter
            fun dateToTimestamp(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)
        abstract class PicturesDatabase: 
                         RoomDatabase() {
    
    0 讨论(0)
  • 2020-12-16 20:11

    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:

    public class Converters {
        @TypeConverter
        public static Date fromTimestamp(Long value) {
            return value == null ? null : new Date(value);
        }
        @TypeConverter
        public static Long dateToTimestamp(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})
    public abstract class AppDatabase extends RoomDatabase {
        public abstract 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.

    0 讨论(0)
提交回复
热议问题