如何使用嵌套类模型创建房间实体?

huangapple go评论65阅读模式
英文:

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 = &quot;daily_table&quot;)
public class DailyWeather {
@PrimaryKey(autoGenerate = true)
private int id;
@SerializedName(&quot;lat&quot;)
private final double lat;
@SerializedName(&quot;lon&quot;)
private final double lon;
@SerializedName(&quot;timezone&quot;)
private final String timezone;
@SerializedName(&quot;timezone_offset&quot;)
private final int timezoneOffset;
@Embedded(prefix = &quot;daily_&quot;)
@SerializedName(&quot;daily&quot;)
private final List&lt;Daily&gt; daily;
public int getId() {
return id;
}
public DailyWeather(int id, double lat, double lon, String timezone, int timezoneOffset, List&lt;Daily&gt; 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&lt;Daily&gt; 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&lt;Daily&gt; getDaily() {
return daily;
}
public static class Daily {
@SerializedName(&quot;dt&quot;)
private final int dt;
@SerializedName(&quot;sunrise&quot;)
private final int sunrise;
@SerializedName(&quot;sunset&quot;)
private final int sunset;
@Embedded
@SerializedName(&quot;temp&quot;)
private final Temp temp;
@Embedded
@SerializedName(&quot;feels_like&quot;)
private final FeelsLike feelsLike;
@SerializedName(&quot;pressure&quot;)
private final int pressure;
@SerializedName(&quot;humidity&quot;)
private final int humidity;
@SerializedName(&quot;dew_point&quot;)
private final double dewPoint;
@SerializedName(&quot;wind_speed&quot;)
private final double windSpeed;
@SerializedName(&quot;wind_deg&quot;)
private final int windDeg;
@Embedded
@SerializedName(&quot;weather&quot;)
private final List&lt;Weather&gt; weather;
@SerializedName(&quot;clouds&quot;)
private final int clouds;
@SerializedName(&quot;pop&quot;)
private final double pop;
@SerializedName(&quot;uvi&quot;)
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&lt;Weather&gt; 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&lt;Weather&gt; getWeather() {
return weather;
}
public int getClouds() {
return clouds;
}
public double getPop() {
return pop;
}
public double getUvi() {
return uvi;
}
public static class Temp {
@SerializedName(&quot;day&quot;)
private final double day;
@SerializedName(&quot;min&quot;)
private final double min;
@SerializedName(&quot;max&quot;)
private final double max;
@SerializedName(&quot;night&quot;)
private final double night;
@SerializedName(&quot;eve&quot;)
private final double eve;
@SerializedName(&quot;morn&quot;)
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(&quot;day&quot;)
private final double day;
@SerializedName(&quot;night&quot;)
private final double night;
@SerializedName(&quot;eve&quot;)
private final double eve;
@SerializedName(&quot;morn&quot;)
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(&quot;id&quot;)
private final int id;
@SerializedName(&quot;main&quot;)
private final String main;
@SerializedName(&quot;description&quot;)
private final String description;
@SerializedName(&quot;icon&quot;)
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&lt;Daily&gt; toList(String value) {
Type listType = new TypeToken&lt;List&lt;Daily&gt;&gt;() {}.getType();
return new Gson().fromJson(value, listType);
}
@TypeConverter
public static String toString(List&lt;Daily&gt; 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 &#39;com.google.code.gson:gson:latest-version&#39;

huangapple
  • 本文由 发表于 2020年9月24日 22:06:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64048227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定