Android: Pass List<GeoPoint> to another Activity
12,531
This function will help you: http://developer.android.com/reference/android/content/Intent.html#putParcelableArrayListExtra(java.lang.String, java.util.ArrayList<? extends android.os.Parcelable>)
But the problem is, that GeoPoint is not Parcelable. Well, you can do a workaround here:
1) Create a class, that implements Parcelable:
public class ParcelableGeoPoint implements Parcelable { private GeoPoint geoPoint; public ParcelableGeoPoint(GeoPoint point) { geoPoint = point; } public GeoPoint getGeoPoint() { return geoPoint; } public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { out.writeInt(geoPoint.getLatitudeE6()); out.writeInt(geoPoint.getLongitudeE6()); } public static final Parcelable.Creator<ParcelableGeoPoint> CREATOR = new Parcelable.Creator<ParcelableGeoPoint>() { public ParcelableGeoPoint createFromParcel(Parcel in) { return new ParcelableGeoPoint(in); } public ParcelableGeoPoint[] newArray(int size) { return new ParcelableGeoPoint[size]; } }; private ParcelableGeoPoint(Parcel in) { int lat = in.readInt(); int lon = in.readInt(); geoPoint = new GeoPoint(lat, lon); } }
2) when sending to the other activity (points is your List<GeoPoint>:
ArrayList<ParcelableGeoPoint> pointsExtra = new ArrayList<ParcelableGeoPoint>(); foreach(GeoPoint point: points) { pointsExtra.add(new ParcelableGeoPoint(point)); } intent.putExtra("geopoints", pointsExtra);
3) in the called activity:
ArrayList<ParcelableGeoPoint> pointsExtra = getIntent().getParcelableArrayListExtra("geopoints"); ArrayList<GeoPoint> points = new ArrayList<GeoPoint>(); foreach(ParcelableGeoPoint point: pointsExtra) { points.add(point.getGeoPoint()); }
code should work, but is untested.
Author by
Rohith Nandakumar
iOS | Android | Java Web Developer | #SOreadytohelp
Updated on July 27, 2022Comments
-
Rohith Nandakumar almost 2 years
I have an ArrayList of type GeoPoint.
private List<GeoPoint> points = new ArrayList<GeoPoint>();
I want to pass
points
to another Activity and retrive the data in that activity. How do I do it? I know I have to use theparcelable
but I searched, but could not find a way to pass ArrayLists. -
Rohith Nandakumar over 13 yearsThanks a million. Im trying to figure this out =D What is
MyParcelable
in this code? -
Patrick Boos over 13 yearsoh sorry. my mistake! MyParcelable is out of the sample on developer.android.com that i used. MyParcelable should have been replaced with ParcelableGeoPoint above. :) just edited to correct the post.
-
Rohith Nandakumar over 13 years:) Thanks. ok you should also change
int long
in ParcelableGeoPoint(Parcel in) :) Thanks again. -
Rohith Nandakumar over 13 yearsWhy does it show the warning
Parcelable.Creator is a raw type. References to generic type Parcelable.Creator<T> should be parameterized
?? Can I ignore it? -
Rohith Nandakumar over 13 yearsAlso, I get an error in the new Activity in the foreach loop
Type mismatch: cannot convert from element type Object to MapsActivity
-
Patrick Boos over 13 years@rohith sorry. i didn't notice that this editor replaces > if it is not code. just learning how to use the editor here on stackoverflow to display my code correct. i think i fixed it now above. all should be displayed correctly.
-
Rohith Nandakumar over 13 yearsI still dint quite understand how this works. Can you please tell me what changes I should make in this to pass
List<String> names
? -
Patrick Boos over 13 yearsTo pass a list of Strings is even easier. but a little different. check out the Intent functions intent.putStringArrayListExtra(...) and intent.getStringArrayListExtra(...). They are all you need. no extra class.
-
Philipp Hanes over 11 years@PatrickBoos Any particular reason not to extend GeoPoint to implement Parcelable? Seems like fewer objects created, and slightly cleaner-looking internal code. I like the IS-A option here rather than the HAS-A.
-
Patrick Boos over 11 years@PhilippHanes no specific reason :) That way would probably be the better way.
-
Shid about 5 yearsPerfect answer, but GeoPoints are of type double not int