I have got a model for "DayForecast" containing a SparseArray of another model "WeatherCondition", which has several info about weatherconditions for every 3 hours (so 8 weathercondition models in there).
DayForecast.java:
public class DayForecast implements Serializable {
private String mDate;
private String mDescription;
private SparseArray<WeatherCondition> mWeatherConditions = new SparseArray<WeatherCondition>();
public WeatherCondition getWeatherCondition(int timeInHours) {
// return null if no weather condition was set
WeatherCondition weatherCondition = mWeatherConditions.get(timeInHours);
// or you could add some other logic here, if you would want the next available weather condition,
// but make sure to reflect that in the method name
return weatherCondition;
}
public void setWeatherCondition(int timeInHours, WeatherCondition weatherCondition) {
mWeatherConditions.append(timeInHours, weatherCondition);
}
public String getmDate() {
return mDate;
}
public void setmDate(String mDate) {
this.mDate = mDate;
}
public String getmDescription() {
return mDescription;
}
public void setmDescription(String mDescription) {
this.mDescription = mDescription;
}
}
WeatherCondition.java
public class WeatherCondition {
public int getTime() {
return mTime;
}
public void setTime(int time) {
this.mTime = time;
}
private int mTime;
private String mTemperature;
private String mWindSpeed;
private String mDirection;
public WeatherCondition(int time,String temperature, String windSpeed, String direction) {
mTime = time;
mTemperature = temperature;
mWindSpeed = windSpeed;
mDirection = direction;
}
// ... setter and getter methods ...
public String getmTemperature() {
return mTemperature;
}
public void setmTemperature(String mTemperature) {
this.mTemperature = mTemperature;
}
public String getmWindSpeed() {
return mWindSpeed;
}
public void setmWindSpeed(String mWindSpeed) {
this.mWindSpeed = mWindSpeed;
}
public String getmDirection() {
return mDirection;
}
public void setmDirection(String mDirection) {
this.mDirection = mDirection;
}
}
Fragment.java:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
dayForecast = (DayForecast) args.getSerializable("object");
}
What I would like to do is to build a listView of WeatherConditions in my fragment.
I successfully retrieved the other data of the DayForecast object passed to my fragment but I'm struggling retrieving the weatherconditions and build the listView.
Can someone help me please?
Thx
A ListView needs an adapter, so the main thing you need to do is to write the adapter that builds the ListView items for each weather condition. Something like this:
Then you just need to attach the adapter to your view. You showed code from your Fragment's onCreate method, but really you should be doing this work in onCreateView:
You may prefer to build the adapter around your DayForecast object rather than the SparseArray of WeatherCondition, but that's an easy change. Also, of course, you'll need the two layout files, for the entire fragment (forecast_fragment), and for each list item (weather_list_item).