0

I have this class:

public class Weather {

public Today today = new Today(){};
public Tomorrow tomorrow = new Tomorrow(){};
public DayAfter dayafter = new DayAfter(){};
public DayDayAfter daydayafter = new DayDayAfter(){};

public class Today {
    private String mToday;
    private int mTodayHours;
    private final ArrayList<String> arrTime1 = new ArrayList<String>();
    private final ArrayList<String> arrWind_Speed1 = new ArrayList<String>();
    private final ArrayList<String> arrCloud_Amount1 = new ArrayList<String>();
    private final ArrayList<String> arrPop1 = new ArrayList<String>();
    private final ArrayList<String> arrWind_Gust1 = new ArrayList<String>();
    private final ArrayList<String> arrTemperature1 = new ArrayList<String>();
    private final ArrayList<String> arrWind_Direction1 = new ArrayList<String>();
    private final ArrayList<String> arrWeather1 = new ArrayList<String>();

    public void putTodayHours(int value){ mTodayHours = value; }
    public int getTodayHours() {return mTodayHours;}
    public String getToday(){ return mToday;}

    public void putToday(String key, String value){
        switch (key) {
            case "today": mToday = value;break;
            case "time":arrTime1.add(value);break;
            case "wind_speed":arrWind_Speed1.add(value);break;
            case "cloud_amount":arrCloud_Amount1.add(value);break;
            case "pop":arrPop1.add(value);break;
            case "wind_gust":arrWind_Gust1.add(value);break;
            case "temperature":arrTemperature1.add(value);break;
            case "wind_direction":arrWind_Direction1.add(value);break;
            case "weather":arrWeather1.add(value);break;
        }
    }
    public ArrayList getToday(String key){
        switch (key){
            case "time": return arrTime1;
            case "wind_speed": return arrWind_Speed1;
            case "cloud_amount": return arrCloud_Amount1;
            case "pop": return arrPop1;
            case "wind_gust": return arrWind_Gust1;
            case "temperature": return arrTemperature1;
            case "wind_direction": return arrWind_Direction1;
            case "weather": return arrWeather1;
        }
        return null;
    }


}

and so on... which is parsed an populated via this AsyncTask:

public class JSONWeatherTask extends AsyncTask<String, Void, Weather> {

    @Override
    protected Weather doInBackground(String... params) {
        String data = ((new WeatherHttpClient()).getWeatherData(params[0], params[1]));
        Weather forecast = new Weather();
        try {
            forecast = JSONWeatherParser.getWeather(data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return forecast;
    }
    @Override
    protected void onPostExecute(Weather weather){
        super.onPostExecute(weather);
        Intent intent = new Intent(getApplicationContext(),FragmentStatePagerSupportActivity.class);
        startActivity(intent);

which on PostExecute calls another activity class (FragmentPagerSupportActivity). The question is: How to pass the already populated "Weather weather" to the activity? Thanks.

3 Answers3

0

An easy way will be to serialize the object, using Gson for example:

 String serializedObject = new Gson().toJson(forecast);

and pass it with intent extra:

intent.putExtra("weatherObject", serializedObject);

Then in the recieving activity deserialize your object:

Type typeOfT = new TypeToken<forecast>() {
        }.getType();

        forecast = new Gson().fromJson(serializedObject , typeOfT);
SuperFrog
  • 7,631
  • 9
  • 51
  • 81
0

What you want is to transfer the weather object to the activity. You can use parcelable for that.

varunkr
  • 5,364
  • 11
  • 50
  • 99
0

After trying the suggestions above I encountered several problems and the code was getting extended beyond my abilities. Instead of Parcelable or Serialized I decided to put the following inner class in the originating activity:

static public class ManageData {
    static Weather weather;
    static public Weather getData(){
        return weather;
    }
    static public void putData(Weather forecast) {
        weather = forecast;

    }
}

and then in the receiving activity:

Weather forecast = splash_activity.ManageData.getData();

And it works. Would someone knowledgeable let me know if I'm shooting in my foot with this approach? Thanks.