2012-12-17 83 views
1

在後臺我解析JSON,然後在這個背景下,我發送JSON到可parcelable 並在Parcelable我writestringJSONObject.optString我怎麼可以寫ArrayList?Android Parcelable創建陣列

List<Row> result = new ArrayList<Row>(array.length()); 
       for (int i = 0; i < array.length(); i++) { 
        result.add(new Row(array.optJSONObject(i))); 
       } 

Parcelable.java

public Row(JSONObject from) { 
     thumb = from.optString(TAG_THUMBNAILS); 
     bigImage = from.optString(TAG_BIG_IMAGE); 
     author = from.optString(TAG_AUTHOR); 
     description = from.optString(TAG_DESCRIPTION); 
     date = from.optString(TAG_DATE); 
    } 

    public Row(Parcel parcel) { 
     thumb = parcel.readString(); 
     bigImage = parcel.readString(); 
     author = parcel.readString(); 
     description = parcel.readString(); 
     date = parcel.readString(); 
    } 

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

    @Override 
    public void writeToParcel(Parcel parcel, int flags) { 
     parcel.writeString(thumb); 
     parcel.writeString(bigImage); 
     parcel.writeString(author); 
     parcel.writeString(description); 
     parcel.writeString(date); 
    } 
+0

chek this,[ArrayList with Parcelable](http://stackoverflow.com/questions/7400564/android-parcelable-retailerorderactivity-java-return-null/7400675#7400675) – RobinHood

回答

1

它是不完整的,但我認爲你會得到點。會盡力協助你。

public class Row implements Parceleable{ 

    private String thumb; 
    private String bigImage; 
    private String author; 
    private String description; 
    private String date; 

// your setters and getters here 
    public void setThumb(String thumb){ 
     this.thumb = thumb; 
    } 
    public String getThumb(){ 
     return thumb; 
    } 
// .... 

    public Row(){ 
    } 

    public Row(Parcel parcel) { 
     thumb = parcel.readString(); 
     bigImage = parcel.readString(); 
     author = parcel.readString(); 
     description = parcel.readString(); 
     date = parcel.readString(); 
    } 

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

    @Override 
    public void writeToParcel(Parcel parcel, int flags) { 
     parcel.writeString(thumb); 
     parcel.writeString(bigImage); 
     parcel.writeString(author); 
     parcel.writeString(description); 
     parcel.writeString(date); 
    } 

    public static final Parcelable.Creator<Row> CREATOR = new Parcelable.Creator<MyParcelable>() { 

    @Override 
    public Row createFromParcel(Parcel in) { 
     return new Row(in); 
    } 

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

填充從你的JSON:

List<Row> myRowArray = new ArrayList<Row>; 

Row result; 
      for (int i = 0; i < array.length(); i++) { 
       result = new Row(); 
       result.setThumb(fromJson); //Set thumb string you got from json 
       result.setAuthor(fromJson); //Set Author string you got from json 
       ..... 
       myRowArray.add(result); 
      } 

這就是我們如何做it.It是很容易的。一開始它不會引起混淆,但如果你瞭解了這一點,你會發現它很簡單,非常方便。