【问题标题】:Write Parcelable for ArrayList<String []> in android?在 android 中为 ArrayList<String []> 编写 Parcelable?
【发布时间】:2013-07-02 04:50:58
【问题描述】:

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

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&lt;String []&gt; 执行操作,并且这些值需要传递给另一个活动。提前致谢。

【问题讨论】:

标签: android arraylist parcelable arrays


【解决方案1】:

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

【讨论】:

  • 感谢朋友,但它会写 list 而不是 ArrayList @S.A.Norton Stanley
【解决方案2】:

我能想到的最简单的方法如下:

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());
        }
    }
}

【讨论】:

    猜你喜欢
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 2012-06-12
    • 2014-04-22
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多