英文:
How to create a Room Entity using nested class model?
问题
我想要为我的 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;
}
// 后续方法...
}
// 后续嵌套类...
}
英文:
I want to use a nested class model for my Room table but when I use it and annotate the inner classes with
@Embedded
I was given a compile error like this:
> Entities and POJOs must have a usable public constructor. You can have
> an empty constructor or a constructor whose parameters match the
> fields (by name and type). - java.util.List
My nested class:
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;
}
}
}
}
答案1
得分: 3
不要在List
中使用@Embedded
。
使用@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'
英文:
Don't use @Embedded
with List
.
Use @TypeConverter
Create the following class first:
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;
}
}
Then in your Database class, add this after your @Database
annotation:
@TypeConverters({DailyConverter.class})
If you have more converters, just separate them with a comma like so:
@TypeConverters({DateTypeConverter.class, AnotherConverter.class, ABC.class})
If you don't have, add this:
implementation 'com.google.code.gson:gson:latest-version'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论