0

I have a Linked List in one activity (A) that I want to share with another Activity (B). The list contains a username of type string and contains coordinates of type LatLng. I am also using Intent and bundle to share data between activities. I tried using Parcelable but unable to figure out how to use it. Here is the code I have:

data.java

public class data implements Parcelable{
    private LatLng coordinates;
    private String name;


    public data() {
        name = null;
        coordinates = null;
    }

    public data(String name, LatLng coordinates)
    {
        this.name = name;
        this.coordinates = coordinates;
    }

    public data(Parcel in) {
        coordinates = in.readParcelable(LatLng.class.getClassLoader());
        name = in.readString();
    }

    public static final Creator<data> CREATOR = new Creator<data>() {
        @Override
        public data createFromParcel(Parcel in) {
            return new data(in);
        }

        @Override
        public data[] newArray(int size) {
            return new data[size];
        }
    };

    public LatLng getLatLng () {
        return coordinates;
    }

    @Override
    public int describeContents() {
        return hashCode();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeParcelable(coordinates, flags);
    }
}

Activity A

public class A extends FragmentActivity implements
    OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    GoogleMap.OnMyLocationButtonClickListener,
    ActivityCompat.OnRequestPermissionsResultCallback {

    Button switchToSeek;
    double mLatitude;
    double mLongitude;

    LinkedList<data> storedData = new LinkedList<>();

    protected void onCreate(Bundle savedInstanceState) {
        ...
        switchToSeek.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getCurrentLocation();

                        Intent intent = new Intent(A.this, B.class);

                        Bundle xy = new Bundle();
                        xy.putDouble("x", mLatitude);
                        xy.putDouble("y", mLongitude);
                        xy.putParcelable("list", storedData); <---------- error: wrong second arugment

                        intent.putExtra("xy", xy);
                        A.this.startActivity(intent);
                    }
                });

Activity B

public class B extends FragmentActivity implements OnMapReadyCallback {

    double mLatitude;
    double mLongitude;
    LatLng current;
    GoogleMap gMap;

    LinkedList <data> copyData = new LinkedList<>();

    @Override
        public void onMapReady(GoogleMap googleMap) {
            gMap = googleMap;

            ...

            Intent intent = getIntent();
            Bundle xy = intent.getBundleExtra("xy");

            if (xy != null) {
                mLatitude = xy.getDouble("x");
                mLongitude = xy.getDouble("y");
            }
           /***** Call linked list here and set equal to copyData *****/

            current = new LatLng(mLatitude, mLongitude);
            gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(current, 18.0f));

        }
Juan Sierra
  • 261
  • 2
  • 13
  • You can convert your list to an array and then to a string. pass that string with intent to the activity, split it with "," and store the elements in another array, finally iterate over that array and add the elements to your arraylist in another activity. – HaroldSer Nov 19 '16 at 03:05

2 Answers2

1

There is no easy way to do that, since LinkedList does not implement serializable or parcelable.

You CAN implement your own linked list class and make it a serializable/parcelable object which can then be passed.

Or you can convert its content into another data type such as an array and then recreate the linkedlist.* THIS IS HIGHLY INEFFICIENT

I believe there are other ways but this is a standard problem in android dev. Maybe try using fragments if possible and passing the linkedlist through a setter()

  • Yea, I've noticed that. All the functions for bundle or intent that I would like to use are for arrayList. And I thought about conversion but like you said it is very inefficient especially if I want to store a lot of data in the list – Juan Sierra Nov 19 '16 at 01:57
  • `LinkedList` implements `Serializable`. https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html – Embid123 Mar 29 '20 at 17:34
0

If the list is not huge, you can do it using the following helper class:

public class ParcelableLinkedList<E extends Parcelable> implements Parcelable {

    private final LinkedList<E> linkedList;

    public final Creator<ParcelableLinkedList> CREATOR = new Creator<ParcelableLinkedList>() {
        @Override
        public ParcelableLinkedList createFromParcel(Parcel in) {
            return new ParcelableLinkedList(in);
        }

        @Override
        public ParcelableLinkedList[] newArray(int size) {
            return new ParcelableLinkedList[size];
        }
    };

    public ParcelableLinkedList(Parcel in) {
        // Read size of list
        int size = in.readInt();
        // Read the list
        linkedList = new LinkedList<E>();
        for (int i = 0; i < size; i++) {
            linkedList.add((E)in.readParcelable(ParcelableLinkedList.class.getClassLoader()));
        }

    }

    public ParcelableLinkedList(LinkedList<E> linkedList) {
        this.linkedList = linkedList;
    }

    LinkedList<E> getLinkedList() {
        return linkedList;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        // Write size of the list
        parcel.writeInt(linkedList.size());
        // Write the list
        for (E entry : linkedList) {
            parcel.writeParcelable(entry, flags);
        }
    }
}

In your onClick() method, add the data to the Bundle like this:

xy.putParcelable("list", new ParcelableLinkedList<data>(storedData));

To extract the data from the Bundle, do this:

copyData = ((ParcelableLinkedList<data>)xy.getParcelable("list")).getLinkedList();

I haven't actually compiled and tested this code, but it should work.

If the list is really huge, you are better off storing it in a static member variable in one class and then just referencing it from the other. This isn't normally the way you want to do things in Android, but it is sometimes more expedient to do this than to serialize and deserialize a huge amount of data just to pass it between 2 activities that have access to the same memory space.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • I got java.util.ConcurrentModificationException on @Override public void writeToParcel(Parcel parcel, int flags) { // Write size of the list parcel.writeInt(linkedList.size()); // Write the list for (E entry : linkedList) { parcel.writeParcelable(entry, flags); } } – Kirk_hehe Jan 27 '18 at 13:02