1

我想为我的 Room 表使用嵌套类模型,但是当我使用它并用注释内部类时

@Embedded我收到这样的编译错误:

实体和 POJO 必须有一个可用的公共构造函数。您可以有一个空的构造函数或参数与字段匹配的构造函数(按名称和类型)。- java.util.List

我的嵌套类:

package com.mmdev.ormanweatherapp.model;

import androidx.room.Embedded;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

import com.google.gson.annotations.SerializedName;

import java.util.List;

@Entity(tableName = "daily_table")
public class DailyWeather {

    @PrimaryKey(autoGenerate = true)
    private int id;

    @SerializedName("lat")
    private final double lat;

    @SerializedName("lon")
    private final double lon;

    @SerializedName("timezone")
    private final String timezone;

    @SerializedName("timezone_offset")
    private final int timezoneOffset;

    @Embedded(prefix = "daily_")
    @SerializedName("daily")
    private final List<Daily> daily;

    public int getId() {
        return id;
    }

    public DailyWeather(int id, double lat, double lon, String timezone, int timezoneOffset, List<Daily> daily) {
        this.id = id;
        this.lat = lat;
        this.lon = lon;
        this.timezone = timezone;
        this.timezoneOffset = timezoneOffset;
        this.daily = daily;
    }

    @Ignore
    public DailyWeather(double lat, double lon, String timezone, int timezoneOffset,
                        List<Daily> daily) {
        this.lat = lat;
        this.lon = lon;
        this.timezone = timezone;
        this.timezoneOffset = timezoneOffset;
        this.daily = daily;
    }

    public double getLat() {
        return lat;
    }

    public double getLon() {
        return lon;
    }

    public String getTimezone() {
        return timezone;
    }

    public int getTimezoneOffset() {
        return timezoneOffset;
    }

    public List<Daily> getDaily() {
        return daily;
    }

    public static class Daily {
        @SerializedName("dt")
        private final int dt;

        @SerializedName("sunrise")
        private final int sunrise;

        @SerializedName("sunset")
        private final int sunset;

        @Embedded
        @SerializedName("temp")
        private final Temp temp;

        @Embedded
        @SerializedName("feels_like")
        private final FeelsLike feelsLike;

        @SerializedName("pressure")
        private final int pressure;

        @SerializedName("humidity")
        private final int humidity;

        @SerializedName("dew_point")
        private final double dewPoint;

        @SerializedName("wind_speed")
        private final double windSpeed;

        @SerializedName("wind_deg")
        private final int windDeg;

        @Embedded
        @SerializedName("weather")
        private final List<Weather> weather;

        @SerializedName("clouds")
        private final int clouds;

        @SerializedName("pop")
        private final double pop;

        @SerializedName("uvi")
        private final double uvi;

        public Daily(int dt, int sunrise, int sunset, Temp temp, FeelsLike feelsLike, int pressure,
                     int humidity, double dewPoint, double windSpeed, int windDeg, List<Weather> weather,
                     int clouds, double pop, double uvi) {
            this.dt = dt;
            this.sunrise = sunrise;
            this.sunset = sunset;
            this.temp = temp;
            this.feelsLike = feelsLike;
            this.pressure = pressure;
            this.humidity = humidity;
            this.dewPoint = dewPoint;
            this.windSpeed = windSpeed;
            this.windDeg = windDeg;
            this.weather = weather;
            this.clouds = clouds;
            this.pop = pop;
            this.uvi = uvi;
        }

        public int getDt() {
            return dt;
        }

        public int getSunrise() {
            return sunrise;
        }

        public int getSunset() {
            return sunset;
        }

        public Temp getTemp() {
            return temp;
        }

        public FeelsLike getFeelsLike() {
            return feelsLike;
        }

        public int getPressure() {
            return pressure;
        }

        public int getHumidity() {
            return humidity;
        }

        public double getDewPoint() {
            return dewPoint;
        }

        public double getWindSpeed() {
            return windSpeed;
        }

        public int getWindDeg() {
            return windDeg;
        }

        public List<Weather> getWeather() {
            return weather;
        }

        public int getClouds() {
            return clouds;
        }

        public double getPop() {
            return pop;
        }

        public double getUvi() {
            return uvi;
        }

        public static class Temp {
            @SerializedName("day")
            private final double day;

            @SerializedName("min")
            private final double min;

            @SerializedName("max")
            private final double max;

            @SerializedName("night")
            private final double night;

            @SerializedName("eve")
            private final double eve;

            @SerializedName("morn")
            private final double morn;

            public Temp(double day, double min, double max, double night, double eve, double morn) {
                this.day = day;
                this.min = min;
                this.max = max;
                this.night = night;
                this.eve = eve;
                this.morn = morn;
            }

            public double getDay() {
                return day;
            }

            public double getMin() {
                return min;
            }

            public double getMax() {
                return max;
            }

            public double getNight() {
                return night;
            }

            public double getEve() {
                return eve;
            }

            public double getMorn() {
                return morn;
            }
        }

        public static class FeelsLike {
            @SerializedName("day")
            private final double day;

            @SerializedName("night")
            private final double night;

            @SerializedName("eve")
            private final double eve;

            @SerializedName("morn")
            private final double morn;

            public FeelsLike(double day, double night, double eve, double morn) {
                this.day = day;
                this.night = night;
                this.eve = eve;
                this.morn = morn;
            }

            public double getDay() {
                return day;
            }

            public double getNight() {
                return night;
            }

            public double getEve() {
                return eve;
            }

            public double getMorn() {
                return morn;
            }
        }


        public static class Weather {
            @SerializedName("id")
            private final int id;

            @SerializedName("main")
            private final String main;

            @SerializedName("description")
            private final String description;

            @SerializedName("icon")
            private final String icon;

            public Weather(int id, String main, String description, String icon) {
                this.id = id;
                this.main = main;
                this.description = description;
                this.icon = icon;
            }

            public int getId() {
                return id;
            }

            public String getMain() {
                return main;
            }

            public String getDescription() {
                return description;
            }

            public String getIcon() {
                return icon;
            }
        }
    }
}


 
4

1 回答 1

3

不要@EmbeddedList. 采用@TypeConverter

首先创建以下类:

public class DailyConverter {
   @TypeConverter
   public static List<Daily> toList(String value) {
      Type listType = new TypeToken<List<Daily>>() {}.getType();
      return new Gson().fromJson(value, listType);
   }

   @TypeConverter
   public static String toString(List<Daily> list) {
      Gson gson = new Gson();
      String json = gson.toJson(list);
      return json;
   }
}

然后在您的数据库类中,在您的@Database注释之后添加: @TypeConverters({DailyConverter.class})

如果您有更多转换器,只需用逗号分隔它们,如下所示: @TypeConverters({DateTypeConverter.class, AnotherConverter.class, ABC.class})

如果没有,请添加以下内容: implementation 'com.google.code.gson:gson:latest-version'

于 2020-09-24T14:42:30.533 回答