0

我有一个“DayForecast”模型,其中包含另一个模型“WeatherCondition”的 SparseArray,它每 3 小时有几个关于天气条件的信息(所以那里有 8 个天气条件模型)。

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;
}
}

片段.java:

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle args = getArguments();
    dayForecast = (DayForecast) args.getSerializable("object");


}

我想做的是在我的片段中构建 WeatherConditions 的 listView。

我成功检索到传递给我的片段的 DayForecast 对象的其他数据,但我正在努力检索天气条件并构建 listView。

有人能帮助我吗?

谢谢

4

1 回答 1

0

ListView 需要一个适配器,因此您需要做的主要事情是编写为每种天气条件构建 ListView 项目的适配器。像这样的东西:

public static class WeatherAdapter extends BaseAdapter {

    private final LayoutInflater inflater;
    private final SparseArray<WeatherCondition> weatherConditions;

    public WeatherAdapter(LayoutInflater inflater, SparseArray<WeatherCondition> weatherConditions) {
        this.inflater = inflater;
        this.weatherConditions = weatherConditions;
    }

    @Override
    public int getCount() {
        return weatherConditions.size();
    }

    @Override
    public WeatherCondition getItem(int position) {
        return weatherConditions.valueAt(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.weather_list_item, parent);
        }
        WeatherCondition weatherCondition = getItem(position);
        //TODO configure the views in weather_list_item using data in weatherCondition
        return convertView;
    }
}

然后,您只需要将适配器附加到您的视图。您展示了 Fragment 的 onCreate 方法中的代码,但实际上您应该在 onCreateView 中完成这项工作:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Bundle args = getArguments();
    dayForecast = (DayForecast) args.getSerializable("object");

    View rootView = inflater.inflate(R.layout.forecast_fragment, container, true);
    ListView weatherLV = (ListView) rootView.findViewById(R.id.weather_list);
    weatherLV.setAdapter(new WeatherAdapter(inflater, dayForecast.getConditions()));
    return rootView;
}

您可能更喜欢围绕 DayForecast 对象而不是 WeatherCondition 的 SparseArray 构建适配器,但这是一个简单的更改。当然,您还需要两个布局文件,用于整个片段 (forecast_fragment) 和每个列表项 (weather_list_item)。

于 2014-11-13T03:42:53.387 回答