How to store sparsearray in a kit

I have a SparseArray<myObject> and want to save it in a package in the onSaveInstanceState method in my activity and restore it to oncreate . I found the putSparseParcelableArray method to place the SparseArray in the package and did this in the onSaveInstanceState method:

 bundle.putSparseParcelableArray("mySparseArray", mySparseArray); 

But the eclipse shows this error:

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

And a quick fix calls the argument mySparsArray before SparseArray<? extends Parcelable> SparseArray<? extends Parcelable> , but if I do this and get in the onCreate method:

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

He gets this error:

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

If this path is wrong, then what is the solution for placing mySparseArray in a package? Any help is appreciated.

+4
source share
2 answers

Your class must implement Parcelable and must have a static final member variable called a CREATOR type Parcelable.Creator<myObject> .

+5
source

You can extend SparsArray to imlement a Serializable, how to use it:

 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; } } 
+8
source

All Articles