简体   繁体   中英

Passing Array List Object between activities using parcelable

I'll first start this off saying i have seen all the other posts that deal with this and have tried all of them.

I'm trying to do the same thing asked in the other posts, which is to pass the values of a ArrayList from one activity to another using Intent.

I feel I have implemented my class(es) correctly. The main class (Route_Class) i am using is below.

public static final Parcelable.Creator<Route_Class> CREATOR = 
        new Parcelable.Creator<Route_Class>()

public Route_Class createFromParcel(Parcel in) 
    { 
        System.out.println("in Parcel In");
        Route_Class route_class = new Route_Class();
        route_class.layout_width2 = in.readString();
        route_class.latitude = in.readDouble();
        route_class.longitude = in.readDouble();
        route_class.startingLat = in.readDouble();
        route_class.startingLong = in.readDouble();
        route_class.name   = in.readString();
        route_class.routeName = in.readString();
        route_class.GeoPoints = in.readString();
        route_class.layout_width = in.readString();
        route_class.layout_height = in.readString();
        route_class.orientation = in.readString();
        route_class.xmlns = in.readString();
        route_class.id = in.readString();
        route_class.clickable = in.readInt() == 0;
        route_class.enabled = in.readInt() == 0;
        route_class.layout_height2 = in.readString();
        route_class.apiKey = in.readString();           

                    Bundle b = in.readBundle(GeoPoints_Class.class.getClassLoader());        
        route_class.geoPoints_arraylist = b.getParcelableArrayList("_geoPoints_arraylist");

        return route_class;
    }


    @Override
    public Route_Class[] newArray(int size) 
    {
        return new Route_Class[size];
    }
};

Here is my second class (GeoPoints_Class) that is used in Route_Class.

 public static final Parcelable.Creator<GeoPoints_Class> CREATOR = 
        new Parcelable.Creator<GeoPoints_Class>() 
        { 
            public GeoPoints_Class createFromParcel(Parcel in) { 
                GeoPoints_Class geoPoints_class = new GeoPoints_Class();
                System.out.println("In Parcel In for GEoPoints");
                geoPoints_class.lat = in.readDouble();
                geoPoints_class.lon = in.readDouble();
                geoPoints_class.location   = in.readString();

                return geoPoints_class;

Next is where I put the objects..

 Intent i = new Intent(getApplicationContext(), route.class);

                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                Bundle b = new Bundle();
                b.putParcelableArrayList("route_Classes_temp", route_Classes);
                i.putExtra("selectiontemp",parent.getItemAtPosition(pos).toString());
                i.putExtras(b);
                startActivityForResult(i, 100);

Lastly, where I attempt to get the objects...

 Bundle b = this.getIntent().getExtras();

    route_Classes = b.getParcelableArrayList("route_Classes_temp");
    userMapSelection = b.getString("selectiontemp");

I've been trying to use this as a resource: http://androidideasblog.blogspot.com/2010/02/passing-list-of-objects-between.html but it does not seem to be working. I'm getting the following error when running the application (any clue what the bad magic number is referencing?):

 11-09 14:48:52.951: E/Bundle(1128): readBundle: trace = java.lang.RuntimeException
 11-09 14:48:52.951: E/Bundle(1128):    at android.os.Bundle.readFromParcelInner(Bundle.java:1580)
 11-09 14:48:52.951: E/Bundle(1128):    at android.os.Bundle.<init>(Bundle.java:82)
 11-09 14:48:52.951: E/Bundle(1128):    at android.os.Parcel.readBundle(Parcel.java:1381)

Thanks in advance!

In one time i try it but didn't work. So you can do that :

You create a class and extend it from Application class. You define list here private. And write getters setter methods. This class keep data until application opened. You can set list at first activity. And read at second activity

You should make sample this class that in your activity (For example your application class name : App):

App app = (App) getApplicationContext();

And you can get list that (For example your list name in Application class : list)

App app = (App) getApplicationContext();
app.getList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM