Retrieve SparseArray data from model

115 views Asked by At

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

1

There are 1 answers

5
Bruce On BEST ANSWER

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:

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

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:

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

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).