从模型中检索SparseArray数据

时间:2014-11-13 00:54:15

标签: java android android-listview model sparse-array

我有#34; DayForecast"包含另一个模型的SparseArray" WeatherCondition",其中每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;
}
}

Fragment.java:

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

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


}

我想要做的是在我的片段中构建一个WeatherConditions列表视图。

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

有人能帮助我吗?

THX

1 个答案:

答案 0 :(得分: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)。