您的位置:首页 > 其它

在 Intent 中传 SparseArray 类型的数据

2016-04-20 20:44 615 查看

Intent 传递对象

Android 中Intent传递类对象提供了两种方式一种是通过 实现Serializable接口 传递对象,一种是通过 实现Parcelable接口 传递对象。

要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递。

Intent中传递这2种对象的方法:

Bundle.putSerializable(Key,Object);  //实现Serializable接口的对象

Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象


SparseArray(稀疏数组)

项目中因为有用到 SparseArray 代替 HashMap 来进行性能优化。

结果在两个 Activity 之间用 Intent 传递 一个包含 SparseArray 类型数据的对象(该对象实现了 Serializable 接口) 时报了以下异常:

Parcelable encountered IOException writing serializable object


原因

在使用 Intent 传递 serializable类型的对象时,必须保证该对象中的所有数据内容都实现serializable接口。

而我传递的对象中的 SparseArray 没有实现serializable接口(一些String、Int等基本数据类型已实现serializable接口)。

解决方案

自定义 SparseArray 实现 serializable 接口 ,来替代 原有的SparseArray 。

代码如下

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;
}

}


参考文章:

/article/4921791.html

http://stackoverflow.com/questions/14899718/how-to-store-sparsearray-in-bundle
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: