簡體   English   中英

Android - Parcelable | putParcelableArrayListExtra("ListItems", listItems);

[英]Android - Parcelable | putParcelableArrayListExtra("ListItems", listItems);

我正在嘗試將對象 MyItem 的 ArrayList 從一個活動傳遞到另一個活動。 據我了解,我需要在 MyItem 類中實現 Parcable。 所以這就是我到目前為止所做的:

public class MyItem implements ClusterItem, Parcelable {
    private LatLng mPosition=null;
    private String aString;

    public MyItem(double lat, double lng, String aString) {
        mPosition = new LatLng(lat, lng);
        this.aString= aString;
    }

    @Override
    public LatLng getPosition() {
        return mPosition;
    }

    public String getString(){
        return aString;
    }


    public int describeContents() {
        return 0;
    }


    public void writeToParcel(Parcel dest, int flags) {
        //dest.writeLngLat ?
        dest.writeString(postID);
    }

    public static final Parcelable.Creator<MyItem> CREATOR
            = new Parcelable.Creator<MyItem>() {
        public MyItem createFromParcel(Parcel in) {
            return new MyItem(in);
        }

        public MyItem[] newArray(int size) {
            return new MyItem[size];
        }
    };

    private MyItem(Parcel in) {
        //mPosition = in.readLngLat ?
        aString = in.readString();
    }
}

第一個問題:我怎么能在 writeToParcel 中寫 LngLat 字段,我怎么能在 MyItem(Parcel in) 構造函數中清除它?

第二個問題:這是否足夠讓這段代碼可以工作?

                    ArrayList<MyItem> listItems = new ArrayList<>();
                    Iterator<MyItem> items = cluster.getItems().iterator();
                    while (items.hasNext()) {
                        listItems.add(items.next());
                    }
                    Intent intent = new Intent(MapsActivity.this, ClusterPosts.class);
                    intent.putParcelableArrayListExtra("oO", listItems);
                    startActivity(intent);

然后在 CluserPosts 中:

    Intent intent = getIntent();
    ArrayList<MyItem> post = intent.getParcelableArrayListExtra("oO");
    for(MyItem item : post){
        Log.d("elaela", item.getString());
    }

將包裹寫為 -

dest.writeDouble(mPosition.latitude);
dest.writeDouble(mPosition.longitude);

您可以僅將緯度和經度寫為雙倍到目的地,然后形成一個位置。

dest.writeDouble(lat);
dest.writeDouble(long);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM