简体   繁体   English

使用parcelable在活动之间传递数组列表对象

[英]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. 我正在尝试做其他帖子中要求的相同操作,即使用Intent将ArrayList的值从一个活动传递到另一个活动。

I feel I have implemented my class(es) correctly. 我觉得我已经正确实现了我的课程。 The main class (Route_Class) i am using is below. 我正在使用的主要类(Route_Class)如下。

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. 这是Route_Class中使用的第二个类(GeoPoints_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. 我一直试图将其用作资源: http : //androidideasblog.blogspot.com/2010/02/passing-list-of-objects-between.html,但它似乎无法正常工作。 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. 您创建一个类,并从Application类对其进行扩展。 You define list here private. 您在此处定义列表私有。 And write getters setter methods. 并编写getters setter方法。 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 = (App) getApplicationContext();

And you can get list that (For example your list name in Application class : list) 然后您可以获得该列表(例如,您在Application类中的列表名称:list)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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