7

私は を持っていて、それを自分のアクティビティのメソッドSparseArray<myObject>でバンドルに保存し、 にonSaveInstanceState復元したいと考えていoncreateます。putSparseParcelableArraySparseArray をバンドルに入れるメソッドを見つけ、メソッドでこれを行いましたonSaveInstanceState

bundle.putSparseParcelableArray("mySparseArray", mySparseArray);

しかし、Eclipseはこのエラーを示しています:

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

そして、クイックフィックスは引数mySparsArraySparseArray<? extends Parcelable>にキャストしていますが、そうしてonCreateメソッドで取得すると:

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

次のエラーが発生します。

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

この方法が間違っている場合、 mySparseArray をバンドルに入れるための解決策は何ですか? どんな助けでも大歓迎です。

4

2 に答える 2

9

SparsArray を拡張して、使用時に 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;
    }


}
于 2014-02-05T10:42:29.323 に答える
7

クラスは実装する必要があり、 typeParcelableの static final メンバー変数が呼び出される必要があります。CREATORParcelable.Creator<myObject>

于 2013-02-15T17:32:50.890 に答える