views:

73

answers:

1

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 (shown below), 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

++++++

LogCat (it crashes in outState.putParcelableArrayList("restoList", (ArrayList)restaurantList.getRestaurants()); )

07-05 19:34:13.846: WARN/ActivityManager(563): Activity pause timeout for HistoryRecord{436950f8 {org.digitalfarm.atable/org.digitalfarm.atable.Atable}}
07-05 19:34:28.336: DEBUG/AndroidRuntime(808): Shutting down VM
07-05 19:34:28.336: WARN/dalvikvm(808): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
07-05 19:34:28.336: ERROR/AndroidRuntime(808): Uncaught handler: thread main exiting due to uncaught exception
07-05 19:34:28.637: DEBUG/dalvikvm(808): GC freed 3337 objects / 259288 bytes in 94ms
07-05 19:34:28.647: ERROR/AndroidRuntime(808): java.lang.RuntimeException: Unable to pause activity {org.digitalfarm.atable/org.digitalfarm.atable.Atable}: java.lang.ClassCastException: java.util.LinkedList
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2840)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2797)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2780)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.access$2000(ActivityThread.java:112)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1699)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.os.Looper.loop(Looper.java:123)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.main(ActivityThread.java:3948)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at java.lang.reflect.Method.invoke(Method.java:521)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at dalvik.system.NativeStart.main(Native Method)
07-05 19:34:28.647: ERROR/AndroidRuntime(808): Caused by: java.lang.ClassCastException: java.util.LinkedList
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at org.digitalfarm.atable.Atable.onSaveInstanceState(Atable.java:254)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.Activity.performSaveInstanceState(Activity.java:1008)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1256)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2822)
07-05 19:34:28.647: ERROR/AndroidRuntime(808):     ... 12 more

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


}
A: 

You are getting the ClassCastException because your list is a LinkedList and you are casting it to an ArrayList which is throwing the exception. There doesn't appear to be anything wrong with your Restaurant class.

You should just use ArrayList for your Parcelable List, I don't see why you would need a separate RestaurantList class.

BrennaSoft
Ok thank you. I'm not sure I understand why it's LinkedList, since it's declared as a new ArrayList<Restaurant>();, but I'll investigate. The reason why I need a RestaurantList class is that I use GSON.
jul