Android SparseArray类中的parcelable
我班上有一只名叫test的Sparsarray 测试实现了parcelable,当我通过对象测试时,我希望能够在两个活动之间检索sparsearray,目的如下:Android SparseArray类中的parcelable,android,sparse-matrix,parcelable,serializable,Android,Sparse Matrix,Parcelable,Serializable,我班上有一只名叫test的Sparsarray 测试实现了parcelable,当我通过对象测试时,我希望能够在两个活动之间检索sparsearray,目的如下: Intent intent = new Intent(); intent.putExtra("myObjectTest", myObjectTest); 我看到有WriteSparsarray方法,但它不适用于我的Sparsarray 我当前的代码: public Test(Parcel in) { thi
Intent intent = new Intent();
intent.putExtra("myObjectTest", myObjectTest);
我看到有WriteSparsarray方法,但它不适用于我的Sparsarray
我当前的代码:
public Test(Parcel in) {
this.id = in.readInt();
// how to get the sparsearray ?
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
// how can I parse my SparseArray ?
}
您可以使用readSparseArray和writeSparseArray 例如:
public Test(Parcel in) {
this.id = in.readInt();
in.readSparseArray(OBJECT_STORED.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeSparseArray(mSparseArray);
}
Bundle bundle = new Bundle();
bundle.putParcelable("myObjectTest", test);
intent.putExtra("myBundleTest",bundle);
编辑
把包裹放在一个包裹里,然后把这个包裹放进箱子里
例如:
public Test(Parcel in) {
this.id = in.readInt();
in.readSparseArray(OBJECT_STORED.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeSparseArray(mSparseArray);
}
Bundle bundle = new Bundle();
bundle.putParcelable("myObjectTest", test);
intent.putExtra("myBundleTest",bundle);
SparseArray存储的是哪种对象?这是我创建的一个类,它实现了parcelable。不,我几乎尝试过了,但它没有像我说的那样工作。类型Parcel中的WriteSparSearray方法不适用于SparseArray的参数检查此链接,希望它能帮助您: