【问题标题】:Android SparseArray in class parcelable类 parcelable 中的 Android SparseArray
【发布时间】:2016-08-01 13:50:07
【问题描述】:

我的班级中有一个名为“test”的稀疏数组。

“test”实现了“parcelable”,当我传递一个带有这样的 Intent 的对象 Test 时,我希望能够在两个活动之间检索我的 sparsearray:

    Intent intent = new Intent();
    intent.putExtra("myObjectTest", myObjectTest);

我看到有“writeSparseArray”方法,但它不适用于我的稀疏数组。

我当前的代码:

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 ?
}

【问题讨论】:

  • SparseArray 存储的是哪种对象?
  • 这是我做的一个类,实现了parcelable。

标签: android sparse-matrix parcelable serializable


【解决方案1】:

您可以使用 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);
}

编辑

将 Parcelable 放入一个 Bundle 中,并将这个 bundle 放入 Intent 中

例子:

Bundle bundle = new Bundle();
bundle.putParcelable("myObjectTest", test);
intent.putExtra("myBundleTest",bundle);    

【讨论】:

  • 不,我几乎尝试过,但它不像我说的那样工作。
  • "Parcel类型中的writeSparseArray(SparseArray)方法不适用于参数(SparseArray)"
  • 查看此链接,希望对您有所帮助:gist.github.com/kaushikgopal/9eea148a2188dc58fe37
猜你喜欢
  • 1970-01-01
  • 2019-02-20
  • 2016-05-24
  • 1970-01-01
  • 2023-04-06
  • 2014-04-22
  • 1970-01-01
  • 2014-07-23
  • 1970-01-01
相关资源
最近更新 更多