這是我的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;
等等我覺得在這裏粘貼所有的模型沒有意義。當然,他們都包含setters和getters。
我不知道(我在網上找不到任何相關主題)如何將數據從多個模型一次傳遞給適配器。
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
方法,但我不知道怎麼辦。有任何想法嗎?