0

i'm working on android project, i want to send an arraylist of object that contain latitude and longitude from a fragment to another fragment to show multiple marker on the map but i get an empty argument.

First Fragment

private ArrayList<locationList> beirutList = new ArrayList<locationList>();
            beirutList.add(new locationList(34.077867,33.76087));
            Fragment mapapp = new MapApp();
            FragmentManager fragmentManager = getFragmentManager();
            Bundle data = new Bundle();
            data.putParcelableArrayList("list", beirutList);
            mapapp.setArguments(data);

fragmentManager.beginTransaction().replace(R.id.content_frame, mapapp, "MappFrag").addToBackStack(null).commit();

Second Fragment

  @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,  Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.map_app, container,
                false);

        data = null;
        if (getArguments() != null) {
            data = getArguments();
            Log.e("MapApp", "data is " + data.toString());

            if (data.getParcelableArrayList("list") != null) {
                beirutList = data.getParcelableArrayList("list");

            } else
                Toast.makeText(rootview.getContext(), "location is empty ", Toast.LENGTH_LONG).show();
        }
        try {
            MapsInitializer.initialize(rootview.getContext());
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }

        mLocationClient = new LocationClient(rootview.getContext(), this, this);
        mLocationClient.registerConnectionCallbacks(this);
        mLocationClient.connect();

        return rootview;
    }

when i debug it the list in the Bundle (data variable) has the size 0

locationList class

 public class locationList implements Parcelable {
        private double latitude;
        private double longitude;

        public static final Parcelable.Creator<locationList> CREATOR = new Parcelable.Creator<locationList>() {
        @Override
        public locationList createFromParcel(Parcel source) {
            return new locationList(source);
        }

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


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

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeDouble(latitude);
        dest.writeDouble(longitude);

    }

    private locationList (Parcel bei){
        this.latitude = bei.readDouble();
        this.longitude = bei.readDouble();

    }

    public locationList(){

    }
    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public locationList(double latitude, double longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }
}
Mohamed Mohaideen AH
  • 2,527
  • 1
  • 16
  • 24
Ali Noureddine
  • 324
  • 5
  • 20

2 Answers2

1

guess your creation from your Parcel is wrong.

Look at this implementation: How can I make my custom objects Parcelable?

replace:

private locationList (Parcel bei){
    this.latitude = bei.readDouble();
    this.longitude = bei.readDouble();
}

with sth like:

public Student(Parcel in){
       String[] data = new String[3];

       in.readStringArray(data);
       this.id = data[0];
       this.name = data[1];
       this.grade = data[2];
   }
Community
  • 1
  • 1
longi
  • 11,104
  • 10
  • 55
  • 89
-2

Your ArrayListhas to implement Parcelable too.

Please see the following solution. It's pretty similar to your approach --> Android ArrayList<MyObject> pass as parcelable

Community
  • 1
  • 1
Kody
  • 1,154
  • 3
  • 14
  • 31
  • No, this is not true – pskink Jan 08 '15 at 10:51
  • It is, because Ali Nrd doesn't want to pass a simple `String Array`. Read the API before you downvote answers! – Kody Jan 08 '15 at 10:56
  • Read the API before answerring, he is using `public void putParcelableArrayList (String key, ArrayList extends Parcelable> value)` it means that `value` is an ArrayList which **contains** `Parcelable` elements, ArrayList itself doesn't need to be `Parcelable` – pskink Jan 08 '15 at 11:00