[英]Passing Array List Object between activities using parcelable
首先,我要說我看過所有其他與此相關的文章並嘗試了所有這些文章。
我正在嘗試做其他帖子中要求的相同操作,即使用Intent將ArrayList的值從一個活動傳遞到另一個活動。
我覺得我已經正確實現了我的課程。 我正在使用的主要類(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];
}
};
這是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;
接下來是我放置對象的位置。
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);
最后,在我嘗試獲取對象的地方...
Bundle b = this.getIntent().getExtras();
route_Classes = b.getParcelableArrayList("route_Classes_temp");
userMapSelection = b.getString("selectiontemp");
我一直試圖將其用作資源: http : //androidideasblog.blogspot.com/2010/02/passing-list-of-objects-between.html,但它似乎無法正常工作。 運行應用程序時出現以下錯誤(任何線索表明錯誤的魔術數字是什么?):
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)
提前致謝!
有一次我嘗試了一下,但是沒有用。 因此,您可以這樣做:
您創建一個類,並從Application類對其進行擴展。 您在此處定義列表私有。 並編寫getters setter方法。 此類保留數據,直到打開應用程序為止。 您可以在第一次活動時設置列表。 並在第二活動中閱讀
您應該在活動中將此類作為示例(例如,您的應用程序類名稱:App):
App app = (App) getApplicationContext();
然后您可以獲得該列表(例如,您在Application類中的列表名稱:list)
App app = (App) getApplicationContext();
app.getList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.