如何通过活动之​​间的双阵列(布尔[] [])?

编程入门 行业动态 更新时间:2024-10-28 15:31:32
本文介绍了如何通过活动之​​间的双阵列(布尔[] [])?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我看不到取得双布尔数组通过传递到另一个活动。我用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]; } }; }

更多推荐

如何通过活动之​​间的双阵列(布尔[] [])?

本文发布于:2023-11-11 04:46:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577416.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布尔   阵列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!