如何将sparsearray存储在bundle中

Ehs*_*san 5 android bundle sparse-array android-activity

我有一个SparseArray<myObject>并希望将它存储onSaveInstanceState在我的活动中的方法包中并将其恢复oncreate.我找到putSparseParcelableArray了将SparseArray放入bundle中的onSaveInstanceState方法,并在方法中执行了此操作:

bundle.putSparseParcelableArray("mySparseArray", mySparseArray);
Run Code Online (Sandbox Code Playgroud)

但是eclips显示了这个错误:

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

快速解决方法是将参数转换mySparsArraySparseArray<? extends Parcelable>,但如果我这样做并在onCreate方法中获取它:

mySparseArray = (SparseArray<myObject>) savedInstanceState.getSparseParcelableArray("mySparseArray");
Run Code Online (Sandbox Code Playgroud)

它收到此错误:

Cannot cast from SparseArray<Parcelable> to SparseArray<myObject>
Run Code Online (Sandbox Code Playgroud)

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

Asa*_*ssi 8

你可以使用它将SparsArray扩展为imizable一个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;
    }


}
Run Code Online (Sandbox Code Playgroud)

  • @Sufian我宁愿拥有更具可读性的代码,而不是微观优化。 (2认同)

Adr*_*lor 6

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