8

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
  • 11,367
  • 11
  • 50
  • 60
  • You should use a service or a static class member. Passing large data objects via extras is not a good idea. – Falmarri Nov 10 '10 at 20:10
  • define large extras :) GeoPoint is an object with just two ints. But you are right. If it is too big, than it is a bad idea. But a list of geopoints is okay i think. – Patrick Boos Nov 11 '10 at 00:40
  • 2
    @Falmarri: static class members are globals, and globals are bad, mmmkay? – hoffmanc Dec 05 '11 at 16:07
  • Passing big extra or using static var. Bad design or bad performance? I'm thinking... – emeraldhieu Jun 04 '12 at 06:29

1 Answers1

25

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.

Harish
  • 3,122
  • 2
  • 31
  • 46
Patrick Boos
  • 6,789
  • 3
  • 35
  • 36
  • Thanks a million. Im trying to figure this out =D What is `MyParcelable` in this code? – Rohith Nandakumar Nov 10 '10 at 08:24
  • 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. – Patrick Boos Nov 10 '10 at 08:27
  • :) Thanks. ok you should also change `int long` in ParcelableGeoPoint(Parcel in) :) Thanks again. – Rohith Nandakumar Nov 10 '10 at 08:42
  • Why does it show the warning `Parcelable.Creator is a raw type. References to generic type Parcelable.Creator should be parameterized` ?? Can I ignore it? – Rohith Nandakumar Nov 10 '10 at 08:44
  • Also, I get an error in the new Activity in the foreach loop `Type mismatch: cannot convert from element type Object to MapsActivity` – Rohith Nandakumar Nov 10 '10 at 09:08
  • @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. – Patrick Boos Nov 11 '10 at 00:38
  • I still dint quite understand how this works. Can you please tell me what changes I should make in this to pass `List names`? – Rohith Nandakumar Nov 11 '10 at 06:49
  • 1
    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. – Patrick Boos Nov 11 '10 at 09:27
  • 1
    @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. – Philipp Hanes Sep 20 '12 at 01:55
  • @PhilippHanes no specific reason :) That way would probably be the better way. – Patrick Boos Sep 20 '12 at 02:59
  • Perfect answer, but GeoPoints are of type double not int – Shid Apr 26 '19 at 14:36