为 ArrayList<String []> 写 Parcelable在安卓中? [英] Write Parcelable for ArrayList&lt;String []&gt; in android?

查看:26
本文介绍了为 ArrayList<String []> 写 Parcelable在安卓中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just created model with String array and array list of string array.Like this

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 做,并且这些值需要传递给另一个活动.提前致谢.

I have implemented parcelbale then write for String [] but how to do for the ArrayList<String []> and that values need to pass to another activity.Thanks in advance.

推荐答案

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

Simplest way I could think about is the following:

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

这篇关于为 ArrayList<String []> 写 Parcelable在安卓中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆