writeSparseArray支持SparseArray< INT []&GT ;? [英] writeSparseArray support SparseArray<int[]>?

查看:225
本文介绍了writeSparseArray支持SparseArray< INT []&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SparseArray℃的参数; INT []> ,想的连载的它。

writeSparseArray(对象)为Parcelable好像不支持 INT []
是否有任何其他的方式来序列化 SparseArray< INT []> ,还是只有改变 INT [] 来的对象?

But the writeSparseArray(Object) for Parcelable seems not support int[]. Is there any other way to serialize the SparseArray<int[]>,Or only change int[] to Object?

推荐答案

我检查Parcel.writeSparseArray()方法,在我看来,有一些问题,因为这种方法应该是通用的像写列表()。它看起来像:

I checked Parcel.writeSparseArray() method and in my opinion there is some issue because this method should be generic like writeList(). It looks like:

public final void writeSparseArray(SparseArray<Object> val)

和应

public final void writeSparseArray(SparseArray<? extends Object> val)

public final <T> void writeSparseArray(SparseArray<T> val)

public final void writeSparseArray(SparseArray val)

所以,你必须实现自己的实现此方法SparseArray对象。我不知道这是最好的解决办法,但你可以试试这个:

So you have to implement your own implementation of this method for SparseArray object. I am not sure that it is the best solution but you can try this:

public void writeSparseArray(Parcel dest, SparseArray<int[]> sparseArray) {
    if (sparseArray == null) {
        dest.writeInt(-1);
        return;
    }
    int size = sparseArray.size();
    dest.writeInt(size);
    int i=0;
    while (i < size) {
        dest.writeInt(sparseArray.keyAt(i));
        dest.writeIntArray(sparseArray.valueAt(i));
        i++;
    }
}

private SparseArray<int[]> readSparseArrayFromParcel(Parcel source){
    int size = source.readInt();
    if (size < 0) {
        return null;
    }
    SparseArray sa = new SparseArray(size);
    while (size > 0) {
        int key = source.readInt();
        int[] value = source.createIntArray();
        sa.put(key, value);
        size--;
    }
    return sa;
}

这篇关于writeSparseArray支持SparseArray&LT; INT []&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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