Android的 - 如何通过活动之间的双阵列(布尔[] [])? [英] Android - How to pass a double array(boolean[][]) between activities?
问题描述
我看不到取得双布尔数组通过到另一个活动。我用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.
我如何去传递一个布尔[] [] activites之间?
How would I go about passing a boolean[][] between activites?
推荐答案
如果你绝对需要一个布尔[] [](而不能只用一台布尔[]传递给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];
}
};
}
这篇关于Android的 - 如何通过活动之间的双阵列(布尔[] [])?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!