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.

Share:
12,531
Rohith Nandakumar
Author by

Rohith Nandakumar

iOS | Android | Java Web Developer | #SOreadytohelp

Updated on July 27, 2022

Comments

  • Rohith Nandakumar
    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 the parcelable but I searched, but could not find a way to pass ArrayLists.

  • Rohith Nandakumar
    Rohith Nandakumar over 13 years
    Thanks a million. Im trying to figure this out =D What is MyParcelable in this code?
  • Patrick Boos
    Patrick Boos over 13 years
    oh 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
    Rohith Nandakumar over 13 years
    :) Thanks. ok you should also change int long in ParcelableGeoPoint(Parcel in) :) Thanks again.
  • Rohith Nandakumar
    Rohith Nandakumar over 13 years
    Why 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
    Rohith Nandakumar over 13 years
    Also, I get an error in the new Activity in the foreach loop Type mismatch: cannot convert from element type Object to MapsActivity
  • Patrick Boos
    Patrick Boos over 13 years
    @rohith sorry. i didn't notice that this editor replaces &gt; 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
    Rohith Nandakumar over 13 years
    I 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
    Patrick Boos over 13 years
    To 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
    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
    Patrick Boos over 11 years
    @PhilippHanes no specific reason :) That way would probably be the better way.
  • Shid
    Shid about 5 years
    Perfect answer, but GeoPoints are of type double not int