Как представить сложный api погоды из темного неба в recyclerView? - PullRequest
0 голосов
/ 28 мая 2020

Это мой класс Model, который довольно сложен:

public class Forecast {

    @SerializedName("currently")
    @Expose
    private static Currently currently;
    @SerializedName("daily")
    @Expose
    private Daily daily;
    @SerializedName("flags")
    @Expose
    private Flags flags;
    @SerializedName("hourly")
    @Expose
    private Hourly hourly;
    @SerializedName("latitude")
    @Expose
    private Double latitude;
    @SerializedName("longitude")
    @Expose
    private Double longitude;
    @SerializedName("minutely")
    @Expose
    private Minutely minutely;
    @SerializedName("offset")
    @Expose
    private Integer offset;
    @SerializedName("timezone")
    @Expose
    private String timezone;

    public static Currently getCurrently() {
        return currently;
    }

    public void setCurrently(Currently currently) {
        this.currently = currently;
    }

    public Daily getDaily() {
        return daily;
    }

    public void setDaily(Daily daily) {
        this.daily = daily;
    }

    public Flags getFlags() {
        return flags;
    }

    public void setFlags(Flags flags) {
        this.flags = flags;
    }

    public Hourly getHourly() {
        return hourly;
    }

    public void setHourly(Hourly hourly) {
        this.hourly = hourly;
    }

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }

    public Minutely getMinutely() {
        return minutely;
    }

    public void setMinutely(Minutely minutely) {
        this.minutely = minutely;
    }

    public Integer getOffset() {
        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public String getTimezone() {
        return timezone;
    }

    public void setTimezone(String timezone) {
        this.timezone = timezone;
    }

}


public class Currently {

    @SerializedName("apparentTemperature")
    @Expose
    private Double apparentTemperature;
    @SerializedName("cloudCover")
    @Expose
    private Double cloudCover;
    @SerializedName("dewPoint")
    @Expose
    private Double dewPoint;
    @SerializedName("humidity")
    @Expose
    private Double humidity;
    @SerializedName("icon")
    @Expose
    private String icon;
    @SerializedName("nearestStormBearing")
    @Expose
    private Integer nearestStormBearing;
    @SerializedName("nearestStormDistance")
    @Expose
    private Integer nearestStormDistance;
    @SerializedName("ozone")
    @Expose
    private Double ozone;
    @SerializedName("precipIntensity")
    @Expose
    private Double precipIntensity;
    @SerializedName("precipProbability")
    @Expose
    private Double precipProbability;
    @SerializedName("pressure")
    @Expose
    private Double pressure;
    @SerializedName("summary")
    @Expose
    private String summary;
    @SerializedName("temperature")
    @Expose
    private Double temperature;
    @SerializedName("time")
    @Expose
    private Integer time;
    @SerializedName("visibility")
    @Expose
    private Double visibility;
    @SerializedName("windBearing")
    @Expose
    private Integer windBearing;
    @SerializedName("windSpeed")
    @Expose
    private Double windSpeed;

    public Double getApparentTemperature() {
        return apparentTemperature;
    }

    public void setApparentTemperature(Double apparentTemperature) {
        this.apparentTemperature = apparentTemperature;
    }

    public Double getCloudCover() {
        return cloudCover;
    }

    public void setCloudCover(Double cloudCover) {
        this.cloudCover = cloudCover;
    }

    public Double getDewPoint() {
        return dewPoint;
    }

    public void setDewPoint(Double dewPoint) {
        this.dewPoint = dewPoint;
    }

    public Double getHumidity() {
        return humidity;
    }

    public void setHumidity(Double humidity) {
        this.humidity = humidity;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Integer getNearestStormBearing() {
        return nearestStormBearing;
    }

    public void setNearestStormBearing(Integer nearestStormBearing) {
        this.nearestStormBearing = nearestStormBearing;
    }

    public Integer getNearestStormDistance() {
        return nearestStormDistance;
    }

    public void setNearestStormDistance(Integer nearestStormDistance) {
        this.nearestStormDistance = nearestStormDistance;
    }

    public Double getOzone() {
        return ozone;
    }

    public void setOzone(Double ozone) {
        this.ozone = ozone;
    }

    public Double getPrecipIntensity() {
        return precipIntensity;
    }

    public void setPrecipIntensity(Double precipIntensity) {
        this.precipIntensity = precipIntensity;
    }

    public Double getPrecipProbability() {
        return precipProbability;
    }

    public void setPrecipProbability(Double precipProbability) {
        this.precipProbability = precipProbability;
    }

    public Double getPressure() {
        return pressure;
    }

    public void setPressure(Double pressure) {
        this.pressure = pressure;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public Double getTemperature() {
        return temperature;
    }

    public void setTemperature(Double temperature) {
        this.temperature = temperature;
    }

    public Integer getTime() {
        return time;
    }

    public void setTime(Integer time) {
        this.time = time;
    }

    public Double getVisibility() {
        return visibility;
    }

    public void setVisibility(Double visibility) {
        this.visibility = visibility;
    }

    public Integer getWindBearing() {
        return windBearing;
    }

    public void setWindBearing(Integer windBearing) {
        this.windBearing = windBearing;
    }

    public Double getWindSpeed() {
        return windSpeed;
    }

    public void setWindSpeed(Double windSpeed) {
        this.windSpeed = windSpeed;
    }

}

  public class Datum {

    @SerializedName("apparentTemperatureMax")
    @Expose
    private Double apparentTemperatureMax;
    @SerializedName("apparentTemperatureMaxTime")
    @Expose
    private Integer apparentTemperatureMaxTime;
    @SerializedName("apparentTemperatureMin")
    @Expose
    private Double apparentTemperatureMin;
    @SerializedName("apparentTemperatureMinTime")
    @Expose
    private Integer apparentTemperatureMinTime;
    @SerializedName("cloudCover")
    @Expose
    private Double cloudCover;
    @SerializedName("dewPoint")
    @Expose
    private Double dewPoint;
    @SerializedName("humidity")
    @Expose
    private Double humidity;
    @SerializedName("icon")
    @Expose
    private String icon;
    @SerializedName("moonPhase")
    @Expose
    private Double moonPhase;
    @SerializedName("ozone")
    @Expose
    private Double ozone;
    @SerializedName("precipIntensity")
    @Expose
    private Double precipIntensity;
    @SerializedName("precipIntensityMax")
    @Expose
    private Double precipIntensityMax;
    @SerializedName("precipProbability")
    @Expose
    private Double precipProbability;
    @SerializedName("pressure")
    @Expose
    private Double pressure;
    @SerializedName("summary")
    @Expose
    private String summary;
    @SerializedName("sunriseTime")
    @Expose
    private Integer sunriseTime;
    @SerializedName("sunsetTime")
    @Expose
    private Integer sunsetTime;
    @SerializedName("temperatureMax")
    @Expose
    private Double temperatureMax;
    @SerializedName("temperatureMaxTime")
    @Expose
    private Integer temperatureMaxTime;
    @SerializedName("temperatureMin")
    @Expose
    private Double temperatureMin;
    @SerializedName("temperatureMinTime")
    @Expose
    private Integer temperatureMinTime;
    @SerializedName("time")
    @Expose
    private Integer time;
    @SerializedName("visibility")
    @Expose
    private Double visibility;
    @SerializedName("windBearing")
    @Expose
    private Integer windBearing;
    @SerializedName("windSpeed")
    @Expose
    private Double windSpeed;
    @SerializedName("precipIntensityMaxTime")
    @Expose
    private Integer precipIntensityMaxTime;
    @SerializedName("precipType")
    @Expose
    private String precipType;

    public Double getApparentTemperatureMax() {
        return apparentTemperatureMax;
    }

    public void setApparentTemperatureMax(Double apparentTemperatureMax) {
        this.apparentTemperatureMax = apparentTemperatureMax;
    }

    public Integer getApparentTemperatureMaxTime() {
        return apparentTemperatureMaxTime;
    }

    public void setApparentTemperatureMaxTime(Integer apparentTemperatureMaxTime) {
        this.apparentTemperatureMaxTime = apparentTemperatureMaxTime;
    }

    public Double getApparentTemperatureMin() {
        return apparentTemperatureMin;
    }

    public void setApparentTemperatureMin(Double apparentTemperatureMin) {
        this.apparentTemperatureMin = apparentTemperatureMin;
    }

    public Integer getApparentTemperatureMinTime() {
        return apparentTemperatureMinTime;
    }

    public void setApparentTemperatureMinTime(Integer apparentTemperatureMinTime) {
        this.apparentTemperatureMinTime = apparentTemperatureMinTime;
    }

    public Double getCloudCover() {
        return cloudCover;
    }

    public void setCloudCover(Double cloudCover) {
        this.cloudCover = cloudCover;
    }

    public Double getDewPoint() {
        return dewPoint;
    }

    public void setDewPoint(Double dewPoint) {
        this.dewPoint = dewPoint;
    }

    public Double getHumidity() {
        return humidity;
    }

    public void setHumidity(Double humidity) {
        this.humidity = humidity;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Double getMoonPhase() {
        return moonPhase;
    }

    public void setMoonPhase(Double moonPhase) {
        this.moonPhase = moonPhase;
    }

    public Double getOzone() {
        return ozone;
    }

    public void setOzone(Double ozone) {
        this.ozone = ozone;
    }

    public Double getPrecipIntensity() {
        return precipIntensity;
    }

    public void setPrecipIntensity(Double precipIntensity) {
        this.precipIntensity = precipIntensity;
    }

    public Double getPrecipIntensityMax() {
        return precipIntensityMax;
    }

    public void setPrecipIntensityMax(Double precipIntensityMax) {
        this.precipIntensityMax = precipIntensityMax;
    }

    public Double getPrecipProbability() {
        return precipProbability;
    }

    public void setPrecipProbability(Double precipProbability) {
        this.precipProbability = precipProbability;
    }

    public Double getPressure() {
        return pressure;
    }

    public void setPressure(Double pressure) {
        this.pressure = pressure;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public Integer getSunriseTime() {
        return sunriseTime;
    }

    public void setSunriseTime(Integer sunriseTime) {
        this.sunriseTime = sunriseTime;
    }

    public Integer getSunsetTime() {
        return sunsetTime;
    }

    public void setSunsetTime(Integer sunsetTime) {
        this.sunsetTime = sunsetTime;
    }

    public Double getTemperatureMax() {
        return temperatureMax;
    }

    public void setTemperatureMax(Double temperatureMax) {
        this.temperatureMax = temperatureMax;
    }

    public Integer getTemperatureMaxTime() {
        return temperatureMaxTime;
    }

    public void setTemperatureMaxTime(Integer temperatureMaxTime) {
        this.temperatureMaxTime = temperatureMaxTime;
    }

    public Double getTemperatureMin() {
        return temperatureMin;
    }

    public void setTemperatureMin(Double temperatureMin) {
        this.temperatureMin = temperatureMin;
    }

    public Integer getTemperatureMinTime() {
        return temperatureMinTime;
    }

    public void setTemperatureMinTime(Integer temperatureMinTime) {
        this.temperatureMinTime = temperatureMinTime;
    }

    public Integer getTime() {
        return time;
    }

    public void setTime(Integer time) {
        this.time = time;
    }

    public Double getVisibility() {
        return visibility;
    }

    public void setVisibility(Double visibility) {
        this.visibility = visibility;
    }

    public Integer getWindBearing() {
        return windBearing;
    }

    public void setWindBearing(Integer windBearing) {
        this.windBearing = windBearing;
    }

    public Double getWindSpeed() {
        return windSpeed;
    }

    public void setWindSpeed(Double windSpeed) {
        this.windSpeed = windSpeed;
    }

    public Integer getPrecipIntensityMaxTime() {
        return precipIntensityMaxTime;
    }

    public void setPrecipIntensityMaxTime(Integer precipIntensityMaxTime) {
        this.precipIntensityMaxTime = precipIntensityMaxTime;
    }

    public String getPrecipType() {
        return precipType;
    }

    public void setPrecipType(String precipType) {
        this.precipType = precipType;
    }

}

Он даже больше. Я написал только часть своего модельного класса.

Я хочу получить информацию о погоде на сегодня и завтра. Теперь это моя деятельность, в которой я вызываю их с помощью модификации в двух разных представлениях карт:

    Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            WeatherService service = retrofit.create(WeatherService.class);
            Call<Forecast> call = service.getWeather(latitude, longitude);
            call.enqueue(new Callback<Forecast>() {

                @Override
                public void onResponse(Call<Forecast> call, @NonNull Response<Forecast> response) {

                    if (response.code() == 200) {
                        Forecast forecast = response.body();
                        assert forecast != null;

                        Calendar calendar = Calendar.getInstance();
                        TimeZone tz = TimeZone.getDefault();
                        calendar.add(Calendar.MILLISECOND, tz.getOffset(calendar.getTimeInMillis()));
                        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss", Locale.getDefault());
                        SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-YYYY", Locale.getDefault());

                        double temptoday = forecast.getCurrently().getTemperature();
                        double pr = forecast.getCurrently().getPressure();
                        double hum = forecast.getCurrently().getHumidity();
                        double prec_prob = forecast.getCurrently().getPrecipProbability();
                        int t = forecast.getCurrently().getTime();
                        double maxtemp_today = forecast.getDaily().getData().get(0).getTemperatureMax();
                        double mintemp_today = forecast.getDaily().getData().get(0).getTemperatureMax();
                        icon_today = forecast.getCurrently().getIcon();

                        java.util.Date currenTimeZone1 =new java.util.Date((long)t*1000);
                        time1= sdf.format(currenTimeZone1);

                        double t1 = (5.0/9.0)*(temptoday-32);
                        double mx_t1 = (5.0/9.0)*(maxtemp_today-32);
                        double mn_t1 = (5.0/9.0)*(mintemp_today-32);

                        desc_today = forecast.getCurrently().getSummary();
                        temp_today=(String.format("%.2f",t1)+(char) 0x00B0+"C");
                        press_today = ("Pressure: " +String.valueOf(pr)+" hPa");
                        humid_today =  ("Humidity: " +String.valueOf(hum)+" %");
                        rainpro_today = ("Rain Probability: " +String.valueOf(prec_prob)+" %");
                        max_min_today = String.format("%.2f",mn_t1)+(char) 0x00B0+"C"+"-" + String.format("%.2f",mx_t1)+(char) 0x00B0+"C";

                        Temp_today.setText(temp_today);
                        description_today.setText(desc_today);
                        pressure_today.setText(press_today);
                        humidity_today.setText(humid_today);
                        rainp_today.setText(rainpro_today);
                        loc1.setText(addressLine);
                        loc2.setText(addressLine);
                        dnt.setText(time1);
                        Min_Max.setText(max_min_today);


                        switch (icon_today)
                        {
                            case "clear-day": lAV1.setAnimation(R.raw.clearday);
                                break;
                            case "clear-night": lAV1.setImageResource(R.drawable.night);
                                break;
                            case "rain": lAV1.setAnimation(R.raw.rain);
                                break;
                            case "partly-cloudy-day": lAV1.setAnimation(R.raw.cloudy);
                                break;
                            case "partly-cloudy-night": lAV1.setAnimation(R.raw.cloudynight);
                                break;
                            default:lAV1.setAnimation(R.raw.dfault);
                                break;
                        }

                        desc_tom = String.valueOf(forecast.getDaily().getData().get(1).getSummary());
                        press_tom = String.valueOf(forecast.getDaily().getData().get(1).getPressure());
                        humid_tom = String.valueOf(forecast.getDaily().getData().get(1).getHumidity());
                        rainpro_tom = String.valueOf(forecast.getDaily().getData().get(1).getPrecipProbability());
                        double tommorow_date = forecast.getDaily().getData().get(1).getTime();
                        double mx_tom = forecast.getDaily().getData().get(1).getTemperatureMax();
                        double mn_tom = forecast.getDaily().getData().get(1).getTemperatureMin();
                        icon_tom = forecast.getDaily().getData().get(1).getIcon();

                        java.util.Date currenTimeZone2 =new java.util.Date((long)tommorow_date*1000);
                        String time2= sdf1.format(currenTimeZone2);

                        double mx_tomm = (5.0/9.0)*(mx_tom-32);
                        double mn_tomm = (5.0/9.0)*(mn_tom-32);

                        description_tom.setText(desc_tom);
                        pressure_tom.setText("Pressure: " +press_tom+" hPa");
                        humidity_tom.setText("Humidity: " +humid_tom+" %");
                        rainp_tom.setText("Rain Probability: " +rainpro_tom+" %");
                        tommorowdate.setText(time2);

                        Min_Max_tomtxt= (String.format("%.2f",mn_tomm) + (char)0x00B0+"C"+"-"+String.format("%.2f",mx_tomm)+(char)0x00B0+"C");

                        Min_Max_tom.setText(Min_Max_tomtxt);

                        switch (icon_tom)
                        {
                            case "clear-day": lAV2.setAnimation(R.raw.clearday);
                                break;
                            case "cloudy": lAV2.setAnimation(R.raw.cloudy);
                                break;
                            case "partly-cloudy-day": lAV2.setAnimation(R.raw.cloudy);
                                break;
                            case "partly-cloudy-night": lAV2.setAnimation(R.raw.cloudynight);
                                break;
                            default:lAV2.setAnimation(R.raw.dfault);
                                break;
                        }
}

           @Override
            public void onFailure(Call<Forecast> call, Throwable t) {

            }

        });
    }

Это работает нормально. Но мне нужно поместить эти значения в два представления карт (только одно видимое за раз) внутри горизонтального представления ресайклера. И я не знаю, как это сделать. Скорее всего, мне нужно передать информацию о прогнозе в виде списка в адаптер (которого у меня нет), а затем использовать Multiview recyclerview. Я не знаю, как двигаться дальше. Будет большим подспорьем, если кто-нибудь направит меня. В принципе, если кто-нибудь может подсказать мне, как написать для этого адаптер ресайклера?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...