如何通过活动之​​间的双阵列(布尔[] [])? [英] How to pass a double array(boolean[][]) between activities?

查看:170
本文介绍了如何通过活动之​​间的双阵列(布尔[] [])?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看不到取得双布尔数组通过传递到另一个活动。我用putExtra,当我找回它,并把它转换为布尔[] [] ,它指出,它不能施放和崩溃。布尔[]然而工作。

I can't see to get a double boolean array to pass through to the another activity. I use putExtra and when I retrieve it and cast it to boolean[][], it states that it can not cast and crashes. Boolean[] works however.

我如何去传递一个布尔[] [] 活动的?

How would I go about passing a boolean[][] between activities?

推荐答案

如果你绝对需要一个布尔[] [](而不能只用一台布尔[]数组传递给Parcel.writeBooleanArray做到这一点()) ,那么正式的方式做,这是包装在一个Parcelable类,做编组/解组那里。

If you absolutely need a boolean[][] (and can't do this with just a flat boolean[] array passed to Parcel.writeBooleanArray()), then the formal way to do this is to wrap it in a Parcelable class and do the marshalling/unmarshalling there.

所以肯定会有一些问题,我会勾画出code,虽然这不是测试。

I'll sketch out the code, though this is not tested so there are certainly to be some issues.

public class BooleanArrayArray implements Parcelable {
    private final boolean[][] mArray;

    public BooleanArrayArray(boolean[][] array) {
        mArray = array;
    }

    private BooleanArrayArray(Parcelable in) {
        boolean[][] array;
        final int N = in.readInt();
        array = new boolean[][N];
        for (int i=0; i<N; i++) {
            array[i] = in.createBooleanArray();
        }
        mArray = array;
    }

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

    @Override
    public void writeToParcel(Parcel out, int flags) {
        final int N = mArray.length;
        out.writeInt(N);
        for (int i=0; i<N; i++) {
            out.writeBooleanArray(mArray[i]);
        }
    }

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

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

这篇关于如何通过活动之​​间的双阵列(布尔[] [])?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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