我正在为每日预测制作模型,但我在创建此模型时遇到了一些麻烦。 它有定期信息(日期,描述......),但我需要它每3个小时有各种信息串......(温度,风速,风向等) 这就是我不知道该怎么做的地方。可能是某种类型的数组但不确定。
到目前为止,这就是我所拥有的:
public class DayForecast implements Serializable{
private String date;
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
THX!
答案 0 :(得分:1)
尝试使用ArrayList<HashMap<String,String>>
。通过这种方式,您可以以更少的麻烦和有组织的方式实现相同的目标。
使用示例:
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> data = new HashMap<String,String>();
data.put("description", your_string_description);
data.put("date", your_string_date);
data.put("other", your_string_other_information);
list.add(data);
获取您的数据:
list.get(0).get("description"); //will return the string description in position 0
list.get(0).get("date"); //will return the string description in position 0 you can convert it to date if needed
请注意,get(0)是你的arraylist中的位置,因为你可以添加更多项目,让我们说说明。
答案 1 :(得分:0)
它听起来不像一个字符串数组会这样做,因为你提到了温度,风速,风向等。我会为它创建另一个模型并将它的实例存储在当前的模型中。
e.g。
public class WeatherCondition {
private double mTemperature;
private double mWindSpeed;
private String mDirection;
public WeatherCondition(double temperature, double windSpeed, String direction) {
mTemperature = temperature;
mWindSpeed = windSpeed;
mDirection = direction;
}
// ... setter and getter methods ...
}
和
public class DayForecast {
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);
}
// ... other setter and getter methods
}