在android片段中发送parcelable arraylist时为空Bundle

时间:2015-01-08 10:12:29

标签: android android-fragments arraylist parcelable

我正在研究android项目,我想发送一个包含从一个片段到另一个片段的纬度和经度的对象的arraylist,以在地图上显示多个标记但我得到一个空参数。

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();

第二片段

  @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;
    }

当我调试它时,Bundle(数据变量)中的列表大小为0

locationList类

 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;
    }
}

2 个答案:

答案 0 :(得分:1)

从您的Parcel猜测您的创作是错误的。

请看这个实现:How can I make my custom objects Parcelable?

取代:

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

跟......一样:

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

       in.readStringArray(data);
       this.id = data[0];
       this.name = data[1];
       this.grade = data[2];
   }

答案 1 :(得分:-2)

您的ArrayList也必须实施Parcelable

请参阅以下解决方案。它与您的方法非常相似 - &gt; Android ArrayList<MyObject> pass as parcelable