如何将sparsearray存储在bundle中

时间:2013-02-15 17:05:57

标签: android android-activity bundle sparse-array

我有SparseArray<myObject>并希望在我的活动中以onSaveInstanceState方法将其存储在捆绑中,并在oncreate中恢复它。我找到了putSparseParcelableArray方法,用于将SparseArray放入bundle中,并在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<? extends Parcelable>,但如果我这样做并在onCreate方法中获取它:

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

收到此错误:

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

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

2 个答案:

答案 0 :(得分:8)

您可以扩展SparseArray以实现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;
    }


}

答案 1 :(得分:6)

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