2013-07-02 39 views
0

我剛剛創建與字符串的array.Like這在Android中爲ArrayList寫入Parcelable <String []>?

public class LookUpModel implements Parcelable 
{ 
    private String [] lookup_header; 
    private ArrayList<String []> loookup_values; 

public void writeToParcel(Parcel dest, int flags) { 

      dest.writeStringArray(getLookup_header()); 

     }; 

} 

我已經實現parcelbale然後寫字符串數組,數組列表模型的String [],但如何爲ArrayList<String []>做和值需要傳遞給另一個活動。提前感謝。

+0

http://androidhub.wordpress.com/2011/08/03/android-intents-for-passing-data-between-activities-部分-3 /。檢查這可能有所幫助。 – Raghunandan

回答

0

最簡單的方法我能想到的是:

public static final class LookUpModel implements Parcelable { 
    private String [] lookup_header; 
    private ArrayList<String []> lookup_values; 

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

    public void writeToParcel(Parcel dest, int flags) { 

     dest.writeStringArray(lookup_header); 

     dest.writeInt(lookup_values.size()); 

     for (String[] array : lookup_values) { 
      dest.writeStringArray(array); 
     } 
    }; 

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

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

    /** 
    * Specific constructor for Parcelable support 
    * @param in 
    */ 
    private LookUpModel(Parcel in) { 
     in.readStringArray(lookup_header); 

     final int arraysCount = in.readInt(); 

     lookup_values = new ArrayList<String[]>(arraysCount); 

     for (int i = 0; i < arraysCount; i++) { 
      lookup_values.add(in.createStringArray()); 
     } 
    } 
} 
+0

謝謝@sandrstar – krishh

相關問題