Hi,

I want to save a list of Parcelable Restaurant objects on
onSaveInstanceState. I created a Restaurant class extending Parcelable
and a RestaurantList object, both shown below. In my main activity I
fill my RestauranList directly from json internet data using GSON:

    restaurantList = gson.fromJson(r, RestaurantList.class);

Now, when I want to save the List of Restaurant objects using

    protected void onSaveInstanceState(Bundle outState) {

        if (restaurantList != null ) {
            outState.putParcelableArrayList("restoList",
(ArrayList<Restaurant>)restaurantList.getRestaurants());
       }

        super.onSaveInstanceState(outState);
    }

I get an error, I guess because Parcelable data are not written. How
should I do it? Do I have to create an array of Parcel objects in my
main activity and fill it by calling writeToParcel for each of the
Restaurant objects?

thanks for any suggestion

Julien



**************************RestaurantList
class**************************

public class RestaurantList {

    private List<Restaurant> restaurants = new
ArrayList<Restaurant>();

    public int getSize() {
        return restaurants.size();
    }

    public List<Restaurant> getRestaurants() {
        return this.restaurants;
    }

    public Restaurant getRestaurant(int i) {
        return this.restaurants.get(i);
    }

    public void setRestaurants(List<Restaurant> restaurants) {
        this.restaurants = restaurants;
    }

    public List<String> getRestaurantNames() {

        List<String> restaurantNames = new ArrayList<String>();

        for (int i=0; i<this.restaurants.size(); i++) {
 
restaurantNames.add(this.restaurants.get(i).getName());
        }

        return restaurantNames;
    }

}

**************************Restaurant class**************************

public class Restaurant implements Parcelable{

    private String name;
    /* more stuff */

    public Restaurant() {}

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void writeToParcel(Parcel out, int flags) {

        out.writeStringArray(new String[] {this.name, /* more
stuff*/});

    }

    public static final Parcelable.Creator CREATOR = new
Parcelable.Creator() {

            public Restaurant createFromParcel(Parcel in) { return new
Restaurant(in); }

            public Restaurant[] newArray(int size) { return new
Restaurant[size]; }

    };


    private Restaurant(Parcel in) {

        String[] stringData = new String[6];

        in.readStringArray(stringData);

        this.name = stringData[0];
                /* more stuff */

    }

    public int describeContents(){
        return 0;
    }


}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to