0

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

Makoto
  • 1,455
  • 4
  • 13
  • 17
  • Could anyone help me with this please? – Makoto Nov 13 '14 at 02:01
  • What part are you specifically confused about? How to build an adapter for your data? Or how to literally add a ListView to your activity? – Ifrit Nov 13 '14 at 02:42
  • How to get each "weatherCondition" objects from the "DayForecast" object and build the listView from them – Makoto Nov 13 '14 at 02:52

1 Answers1

0

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

Bruce
  • 2,377
  • 1
  • 17
  • 11
  • Thx!! But for some reason it doesnt work as I get this error "The specified child already has a parent. You must call removeView() on the child's parent first.", I'm using a ViewPager... – Makoto Nov 13 '14 at 07:12
  • What line do you get the error on? I would need more info about your layout. Is your "Fragment.java" a fragment that is on one of your ViewPager's pages? Or is it a parent fragment that contains the ViewPager? You might need to add more detail about that part. – Bruce Nov 13 '14 at 07:26
  • Thx!! Yes, the fragment is one of the ViewPager's pages. I get an error in my main Activity (hosting the AsyncTask), in the onPostExecute Method, when I set the pagerAdapter in the viewPager: @Override protected void onPostExecute(DayForecast[] strings) { if (strings != null) { mPagerAdapter = new PagerAdapter(getFragmentManager(), strings); mViewPager = (ViewPager) findViewById(R.id.pager); // Set the Adapter mViewPager.setAdapter(mPagerAdapter); } } – Makoto Nov 13 '14 at 07:45
  • Ohhh, do you mean that you have one of your WeatherCondition on each page of your ViewPager? This is quite different: you had said ListView, so I thought you meant that you add all WeatherCondition objects in a list on one of your pages. In that case you would need a different type of adapter: a FragmentPagerAdapter. I guess you will instead want multiple instances of the same WeatherFragment class, each showing one WeatherCondition object. Sorry for the confusion. There is a good example in the docs here: http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html – Bruce Nov 13 '14 at 07:55
  • Yes indeed. Each Page of the ViewPager has a listView listing each of the weatherCondition object's time – Makoto Nov 13 '14 at 08:09