How to pass a double array(boolean[][]) between activities?

前端 未结 3 1929
难免孤独
难免孤独 2021-01-25 00:34

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

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-25 01:35

    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.

    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 CREATOR
                = new Parcelable.Creator() {
            public BooleanArrayArraycreateFromParcel(Parcel in) {
                return new BooleanArrayArray(in);
            }
    
            public BooleanArrayArray[] newArray(int size) {
                return new BooleanArrayArray[size];
            }
        };
    }
    

提交回复
热议问题