改造,多个模型到一个适配器

时间:2016-11-08 19:44:27

标签: android retrofit

这是我的json:

{
  "city": {
    "id": 2643743,
    "name": "London",
    "coord": {
      "lon": -0.12574,
      "lat": 51.50853
    },
    "country": "GB",
    "population": 0
  },
  "cod": "200",
  "message": 0.3461,
  "cnt": 3,
  "list": [
    {
      "dt": 1478602800,
      "temp": {
        "day": 3.34,
        "min": 1.05,
        "max": 3.34,
        "night": 1.74,
        "eve": 1.05,
        "morn": 3.34
      },
      "pressure": 1015.27,
      "humidity": 84,
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "speed": 2.23,
      "deg": 257,
      "clouds": 68,
      "rain": 1.21
    },
    {
      "dt": 1478689200,
      "temp": {
        "day": 7.51,
        "min": 1.98,
        "max": 7.62,
        "night": 1.98,
        "eve": 5,
        "morn": 6.51
      },
      "pressure": 999.04,
      "humidity": 98,
      "weather": [
        {
          "id": 502,
          "main": "Rain",
          "description": "heavy intensity rain",
          "icon": "10d"
        }
      ],
      "speed": 4.47,
      "deg": 80,
      "clouds": 92,
      "rain": 25.27
    },
    {
      "dt": 1478775600,
      "temp": {
        "day": 7.38,
        "min": 3.2,
        "max": 8.67,
        "night": 3.87,
        "eve": 6.14,
        "morn": 3.2
      },
      "pressure": 1013.52,
      "humidity": 99,
      "weather": [
        {
          "id": 501,
          "main": "Rain",
          "description": "moderate rain",
          "icon": "10d"
        }
      ],
      "speed": 4.93,
      "deg": 264,
      "clouds": 88,
      "rain": 4.72
    }
  ]
}

由于我想在这里检索的一些数据是嵌套的,我已经制作了多个模型。

public class Response {

    @SerializedName("city") 
    @Expose
    private City city;
    @SerializedName("cod")
    @Expose
    private String cod;
    @SerializedName("message")
    @Expose
    private Double message;
    @SerializedName("cnt")
    @Expose
    private Integer cnt;
    @SerializedName("list")
    @Expose
    private List<WeatherList> list = new ArrayList<WeatherList>();


public class WeatherList {

   @SerializedName("dt")
    @Expose
    private Integer dt;
    @SerializedName("temp")
    @Expose
    private Temp temp;
    @SerializedName("pressure")
    @Expose
    private Double pressure;
    @SerializedName("humidity")
    @Expose
    private Integer humidity;
    @SerializedName("weather")
    @Expose
    private List<Weather> weather = new ArrayList<Weather>();
    @SerializedName("speed")
    @Expose
    private Double speed;
    @SerializedName("deg")
    @Expose
    private Integer deg;
    @SerializedName("clouds")
    @Expose
    private Integer clouds;
    @SerializedName("rain")
    @Expose
    private Double rain;

等。我认为在这里粘贴所有模型毫无意义。当然,所有这些都包含制定者和吸气剂。

我不知道(我在网上找不到任何相关主题)如何将数据从多个模型一次传递到适配器。

public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ForecastViewHolder> {
    private List<Response> forecasts;
    private int rowLayout;
    private Context context;

    constructor, getters and setters

    @Override
    public ForecastAdapter.ForecastViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
        return new ForecastViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ForecastViewHolder holder, final int position) {
        ??????
    }

    @Override
    public int getItemCount() {
        return forecasts.size();
    }


    public static class ForecastViewHolder extends RecyclerView.ViewHolder {
        LinearLayout forecastLayout;
        TextView date;
        TextView tempDay;
        TextView tempNight;
        TextView tempMin;
        TextView tempMax;

        public ForecastViewHolder(View view) {
            super(view);
            forecastLayout = (LinearLayout) view.findViewById(R.id.forecast_item_layout);
            date = (TextView) view.findViewById(R.id.date);
            tempDay = (TextView) view.findViewById(R.id.temp_day);
            tempNight = (TextView) view.findViewById(R.id.temp_night);
            tempMin = (TextView) view.findViewById(R.id.temp_min);
            tempMax = (TextView) view.findViewById(R.id.temp_max);
        }
    }

}

我想从所有模型传递数据并在onBindViewHolder方法中设置它,但我不知道如何。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

在适配器中创建构造函数。获得响应时创建适配器

public ForecastAdapter(Context context,List<Response> forecasts) {
    this.forecasts = forecasts;
    this.context=context;
}

和onbindViewHolder

@Override
public void onBindViewHolder(ForecastViewHolder holder, final int position) {
    holder.date.setText(forecasts.get(position).getDate());
}

以这种方式为每个textview设置数据。

答案 1 :(得分:0)

更改字段

private List<Response> forecasts;private Response response;

getItemCount()更改

return forecasts.size();返回return response.getList().size();

将班级WeatherList重命名为更直观的DayForecast

最后

@Override
public void onBindViewHolder(ForecastViewHolder holder, final int position) {
    DayForecast dayForecast = response.getList().get(position);
    holder.date.setText(convertToString(dayForecast.getDt()));
    holder.tempDay.setText(dayForecast.getTemp().toString));
    //and so on..
}