2012-06-08 43 views
23

Mã hiện được sửa đổi để phản ánh giải pháp được chấp nhận.Android ArrayList <MyObject> chuyển thành bưu kiện

Điều này bây giờ là một ví dụ làm việc về cách chuyển ArrayList tùy chỉnh thành DialogFragment.

Tôi đang chuyển một ArrayList của các đối tượng tùy chỉnh tới DialogFragment bằng cách sử dụng Gói trên newInstance. Danh sách mảng được nhận chính xác trong newInstance. Lệnh gọi putParcelable thực thi tốt (không có lỗi), nhưng việc đặt các điểm ngắt trong mã có thể kiện trong đối tượng ArrayList cho thấy rằng các phương thức bưu kiện không được gọi khi thiết lập hoặc nhận dữ liệu.

Tôi có chính xác khi tạo lớp LocalityList cho ArrayList và làm cho bưu kiện đó có thể chuyển nhượng được hay bản thân lớp Địa phương có thể được chuyển nhượng?

DialogFragment

/** 
* Create a new instance of ValidateUserEnteredLocationLocalitySelectorFragment, providing "localityList" 
* as an argument. 
*/ 
public static ValidateUserEnteredLocationLocalitySelectorFragment newInstance(LocalityList localityList) { 

    ValidateUserEnteredLocationLocalitySelectorFragment fragmentInstance = new ValidateUserEnteredLocationLocalitySelectorFragment(); 

    // Supply location input as an argument. 
    Bundle bundle = new Bundle(); 
    bundle.putParcelable(KEY_LOCALITY_LIST, localityList); 
    fragmentInstance.setArguments(bundle); 

    return fragmentInstance; 
} 


/** 
* Retrieve the locality list from the bundle 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mLocalityList = getArguments().getParcelable(KEY_LOCALITY_LIST); 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.validate_user_entered_location, container, false); 

    mLocalityListView = (ListView) view.findViewById(R.id.dialogLocalityListView); 
    mAdapter = new SearchLocationLocalitiesListAdapter(getActivity(), mLocalityList); 
    mLocalityListView.setAdapter(mAdapter); 

    return view; 
} 

lớp LocalityList

import java.util.ArrayList; 

import android.os.Parcel; 
import android.os.Parcelable; 

public class LocalityList extends ArrayList<Locality> implements Parcelable { 

    private static final long serialVersionUID = 663585476779879096L; 

    public LocalityList() { 
    } 

    @SuppressWarnings("unused") 
    public LocalityList(Parcel in) { 
     this(); 
     readFromParcel(in); 
    } 

    private void readFromParcel(Parcel in) { 
     this.clear(); 

     // First we have to read the list size 
     int size = in.readInt(); 

     for (int i = 0; i < size; i++) { 
      Locality r = new Locality(in.readString(), in.readDouble(), in.readDouble()); 
      this.add(r); 
     } 
    } 

    public int describeContents() { 
     return 0; 
    } 

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

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

    public void writeToParcel(Parcel dest, int flags) { 
     int size = this.size(); 

     // We have to write the list size, we need him recreating the list 
     dest.writeInt(size); 

     for (int i = 0; i < size; i++) { 
      Locality r = this.get(i); 

      dest.writeString(r.getDescription()); 
      dest.writeDouble(r.getLatitude()); 
      dest.writeDouble(r.getLongitude()); 
     } 
    } 
} 

lớp Địa phương

import android.os.Parcel; 
import android.os.Parcelable; 


public class Locality implements Parcelable { 

    private String mDescription; 
    private double mLatitude; 
    private double mLongitude; 


    public Locality(String description, double latitude, double longitude) { 
     super(); 
     this.mDescription = description; 
     this.mLatitude = latitude; 
     this.mLongitude = longitude; 
    } 

    public Locality(){ 
     super(); 
    } 


    public String getDescription() { 
     return mDescription; 
    } 

    public void setDescription(String description) { 
     this.mDescription = description; 
    } 


    public double getLatitude() { 
     return mLatitude; 
    } 

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


    public double getLongitude() { 
     return mLongitude; 
    } 

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


    @SuppressWarnings("unused") 
    public Locality(Parcel in) { 
     this(); 
     readFromParcel(in); 
    } 

    private void readFromParcel(Parcel in) { 
     this.mDescription = in.readString(); 
     this.mLatitude = in.readDouble(); 
     this.mLongitude = in.readDouble(); 
    } 

    public int describeContents() { 
     return 0; 
    } 

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

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


    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(mDescription); 
     dest.writeDouble(mLatitude); 
     dest.writeDouble(mLongitude); 
    } 
} 

Trả lời

7

Vâng, làm Locality lớp tự Parcelable, và đừng quên khởi tạo

ArrayList<Locality> mList= new ArrayList<Locality>(); 
+0

Cảm ơn. Các lớp LocalityList và Localality đều cần phải được chuyển nhượng, hoặc chỉ là chính lớp Địa phương. Tôi đã mong đợi rằng tôi cần phải gửi bưu kiện ArrayList và sẽ nối tiếp tất cả các lớp riêng lẻ trong mảng. – MartinS

+0

có, bạn phải thực hiện kiện trên cả hai. Và serializable là dành cho Java (không được khuyến cáo cho Android) luôn sử dụng thay thế được. –

+0

Mã trên đã được sửa đổi để triển khai giải pháp được chấp nhận. Bây giờ làm việc chính xác như được hiển thị.Điều này chứng tỏ làm thế nào để vượt qua một ArrayList tùy chỉnh vào một FragmentDialog để hiển thị trong một ListView. – MartinS

-1

Bí quyết tôi thường sử dụng là phân tích cú pháp danh sách thành Json bằng Gson (từ google). Ở phía bên kia tôi chỉ chia chuỗi trong Json trở lại một danh sách mới.

Không bao giờ nhận thấy bất kỳ độ trễ nào.

+2

Một ít thông tin hơn luôn luôn tốt (mẫu mã nhỏ để chứng minh). Hiện tại câu trả lời của bạn không giống như câu trả lời cho câu hỏi. – Styxxy

+2

http://stackoverflow.com/questions/4318458/how-to-deserialize-a-list-using-gson-or-another-json-to-java Về cơ bản đây là những gì tôi thường làm. Danh sách -> Chuỗi JSON; Gói; Chuỗi JSON -> Danh sách; – blindado

9

Tôi biết câu hỏi này khá cũ nhưng vì ban đầu tôi đến đây để tìm câu trả lời, tôi muốn chia sẻ kinh nghiệm của mình.

Có, bạn cần triển khai Bưu kiện cho lớp học Locality của bạn nhưng đúng vậy.

Nếu LocalityList CHỈ là trình bao bọc cho ArrayList, thì bạn không cần nó.

Chỉ cần sử dụng phương thức putParcelableArrayList.

ArrayList<Locality> localities = new ArrayList<Locality>; 
... 
Bundle bundle = new Bundle(); 
bundle.putParcelableArrayList(KEY_LOCALITY_LIST, localities); 
fragmentInstance.setArguments(bundle); 

return fragmentInstance; 

Và lấy nó bằng cách sử ...

localities = savedInstanceState.getParcelableArrayList(KEY_LOCALITY_LIST); 

Vì vậy, trừ khi bạn cần ArrayList tùy chỉnh đối với một số lý do nào khác, bạn có thể tránh làm bất kỳ điều đó làm thêm và chỉ thực hiện Parcelable cho Địa phương của bạn lớp học.

Các vấn đề liên quan