0

我刚刚用字符串数组和字符串数组的数组列表创建了模型。像这样

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 [] 编写,但是如何为 theArrayList<String []>和值传递给另一个活动。在此先感谢。

4

2 回答 2

0

使用 dest.writeStringList(loookup_values); 参考以下 http://developer.android.com/reference/android/os/Parcel.html#writeStringList(java.util.List ) 希望有帮助。

于 2013-07-02T05:03:36.690 回答
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());
        }
    }
}
于 2013-07-02T05:30:15.227 回答