如何sparsearray存放在包放在、sparsearray

2023-09-12 22:21:48 作者:时光浅巷

我有一个 SparseArray< myObject的> ,并希望将其存储在包中的的onSaveInstanceState 方法,在我的活动,在的OnCreate 恢复。我在捆绑发现 putSparseParcelableArray 方法把SparseArray和的onSaveInstanceState 这样做的方法:

I have a SparseArray<myObject> and want to store it in bundle in onSaveInstanceState method in my activity and restore it in oncreate. I found putSparseParcelableArray method for put SparseArray in bundle and did this in onSaveInstanceState method:

bundle.putSparseParcelableArray("mySparseArray", mySparseArray);

但显示文摘此错误:

But eclips shows this error:

The method putSparseParcelableArray(String, SparseArray<? extends Parcelable>) in the type Bundle is not applicable for the arguments (String, SparseArray<myObject>)

和快速修复被铸造的说法 mySparsArray SparseArray&LT ;?扩展Parcelable&GT; ,但如果我这样做,并把它在onCreate方法:

And the quick fix is casting argument mySparsArray to SparseArray<? extends Parcelable>, but if I do so and get it in onCreate method:

mySparseArray = (SparseArray<myObject>) savedInstanceState.getSparseParcelableArray("mySparseArray");

它得到这个错误:

It gets this error:

Cannot cast from SparseArray<Parcelable> to SparseArray<myObject>

如果这样是错的,什么是在包放mySparseArray解决? 任何帮助将是非常美联社preciated。

If this way is wrong, what is the solution for put mySparseArray in bundle? Any help would be much appreciated.

推荐答案

您的类实现 Parcelable ,应该有所谓的静态最终成员变量 CREATOR 类型 Parcelable.Creator&LT; myObject的&GT;

Your class should implement Parcelable and should have a static final member variable called CREATOR of type Parcelable.Creator<myObject>.