2013-02-15 33 views
4

我有一個SparseArray<myObject>,並希望在我的活動中將其存儲在onSaveInstanceState方法中,並在oncreate中將其恢復。我在包中發現putSparseParcelableArray方法把SparseArray和onSaveInstanceState方法做:如何在包中存儲sparsearray

bundle.putSparseParcelableArray("mySparseArray", mySparseArray); 

但是文摘顯示了這個錯誤:

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放入捆綁包的解決方案是什麼? 任何幫助將不勝感激。

+0

什麼是'myObject'?它是否實現了Parcelable? – Wenhui 2013-02-15 17:21:12

+0

這是一個自定義的類,我定義並不執行任何操作。它應該實現Parcelable嗎? – Ehsan 2013-02-15 17:30:54

+2

是的,看看參數'putSparseParcelableArray',它是'SparseArray <?擴展了Parcelable>',所以只有實現了Parcelable的對象纔可以放入該包中。你是否需要如何實現'Parcelable'的幫助,這是非常簡單的。 – Wenhui 2013-02-15 17:33:01

回答

5

您的班級應執行Parcelable,並且應該有一個名爲CREATOR的靜態最終成員變量Parcelable.Creator<myObject>

+0

謝謝@Adrian。 – Ehsan 2013-02-16 11:29:15

8

可以擴展到SparsArray一個imlement作爲序列化使用它:

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

for循環從後到前工作的任何好理由?不能只是一個普通的'for(int i = 0; i Diederik 2014-09-08 09:06:05

+1

@Diederik [倒數比倒數快嗎?](http://stackoverflow.com/questions/2823043/is-it-faster-to-count-down-than-it-is-to - 計數) – Sufian 2015-01-29 07:48:15

+1

@Sufian我希望有更多的可讀代碼,通過微觀優化。 – Diederik 2015-01-30 15:27:11