Best way to store SparseBooleanArray in Bundle?

2019-02-03 07:40发布

When a config change happens, my ListView Checkbox states get lost, which I understand why. I try to implement

public void onSaveInstanceState(final Bundle outState)

in one of my Fragments. So I'm just wondering what's the easiest way to store my SparseBooleanArray in the outState.

Also, I'm a bit confused, as ListView has the method:

getListView().getCheckedItemPositions();

What's this good for?

8条回答
迷人小祖宗
2楼-- · 2019-02-03 08:15

Extend SparseArray to implement a 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;
    }


}
查看更多
仙女界的扛把子
3楼-- · 2019-02-03 08:15

I am trying to do the same thing. Initially i was using a HashMap

How to send hashmap value to another activity using an intent

to pass between activities since it extends the Serializable interface. However, after doing some research:

"SparseBooleanArrays are intended to be more efficient than using a HashMap to map Integers to Booleans."

However, I can't find a simple way to store this data structure. I have contemplated grabbing the values of the SparseBooleanArray and storing it in a HashMap so that it can be passed between activities. But this seems to add more complexity.

Unfortunately, I think I am going to revert to using HashMaps to store my list of checked boxes

查看更多
登录 后发表回答