【问题标题】:How to store sparsearray in bundle如何将稀疏数组存储在包中
【发布时间】:2013-01-31 17:22:50
【问题描述】:

我有一个SparseArray<myObject> 并希望将其存储在我活动中onSaveInstanceState 方法的捆绑包中,并将其恢复到oncreate。我发现 putSparseParcelableArray 方法将 SparseArray 放入捆绑包中,并在 onSaveInstanceState 方法中执行此操作:

bundle.putSparseParcelableArray("mySparseArray", mySparseArray);

但 eclips 显示此错误:

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

快速解决方法是将参数 mySparsArray 转换为 SparseArray&lt;? extends Parcelable&gt;,但如果我这样做并在 onCreate 方法中获取它:

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

得到这个错误:

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

如果这种方式是错误的,将 mySparseArray 放在 bundle 中的解决方案是什么? 任何帮助将非常感激。

【问题讨论】:

  • 什么是myObject?是否实现Parcelable
  • 这是我定义的自定义类,没有实现任何东西。它应该实现 Parcelable 吗?
  • 是的,看参数putSparseParcelableArray,是SparseArray&lt;? extends Parcelable&gt;,所以只有实现了Parcelable的对象才能放入bundle。是否需要关于如何实现Parcelable 的帮助,非常简单。
  • Here 就是一个例子。
  • 谢谢@Wenhui,它工作正常。

标签: android android-activity bundle sparse-array


【解决方案1】:

您可以扩展 SpasArray 以实现 Serializable 以使用它:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import android.util.SparseArray;



/**
 * @author Asaf Pinhassi www.mobiledev.co.il
 * @param <E>
 *
 */
public class SerializableSparseArray<E> extends SparseArray<E> implements Serializable{

    private static final long serialVersionUID = 824056059663678000L;

    public SerializableSparseArray(int capacity){
        super(capacity);
    }

    public SerializableSparseArray(){
        super();
    }

    /**
     * This method is private but it is called using reflection by java
     * serialization mechanism. It overwrites the default object serialization.
     *
     * <br/><br/><b>IMPORTANT</b>
     * The access modifier for this method MUST be set to <b>private</b> otherwise {@link java.io.StreamCorruptedException}
     * will be thrown.
     *
     * @param oos
     *            the stream the data is stored into
     * @throws IOException
     *             an exception that might occur during data storing
     */
    private void writeObject(ObjectOutputStream oos) throws IOException {
        Object[] data = new  Object[size()];

        for (int i=data.length-1;i>=0;i--){
            Object[] pair = {keyAt(i),valueAt(i)}; 
            data[i] = pair;
        }
        oos.writeObject(data);
    }

    /**
     * This method is private but it is called using reflection by java
     * serialization mechanism. It overwrites the default object serialization.
     *
     * <br/><br/><b>IMPORTANT</b>
     * The access modifier for this method MUST be set to <b>private</b> otherwise {@link java.io.StreamCorruptedException}
     * will be thrown.
     *
     * @param oos
     *            the stream the data is read from
     * @throws IOException
     *             an exception that might occur during data reading
     * @throws ClassNotFoundException
     *             this exception will be raised when a class is read that is
     *             not known to the current ClassLoader
     */
    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        Object[] data = (Object[]) ois.readObject();
        for (int i=data.length-1;i>=0;i--){
            Object[] pair = (Object[]) data[i]; 
            this.append((Integer)pair[0],(E)pair[1]);
        }
        return;
    }


}

【讨论】:

  • for 循环从后到前工作有什么好的理由吗?不能只是普通的for (int i = 0; i &lt; data.length; i++)吗?
  • @Sufian 比起微优化,我更喜欢可读性更高的代码。
  • @Diederik 我也是,但因为我不觉得更难,所以我不抱怨。
【解决方案2】:

您的类应该实现Parcelable,并且应该有一个名为CREATOR 类型为Parcelable.Creator&lt;myObject&gt; 的静态最终成员变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 2018-08-22
    • 2013-02-06
    • 1970-01-01
    相关资源
    最近更新 更多