问题描述
在我的Android应用程序中,我已经将一些字符串保存到SharedPreferences中,并使用Strings序列化了ArrayList,以便保存此数据并将其用于将来.即使关闭该应用程序.一分钟前,我发现我还需要保存PolylineOptions以便将来使用. PolylineOptions包含一些坐标,可以在我的地图上用颜色和宽度绘制一条线.
In my Android app I am already saving some strings to the SharedPreferences and serializing an ArrayList with Strings so this data is saved and can be used for future purposes. Even when the app is closed.A minute ago I discovered that I need to save my PolylineOptions for future use as well. PolylineOptions contain some coordinates to draw a line on my map with a color and width.
我发现PolylineOptions不能像Strings一样可序列化.有没有一种方法可以保存"我的PolylineOptions,还是需要在启动时保存PolylineOptions的设置并创建PolylineOptions?
I discovered that PolylineOptions aren't serializeable like Strings. Is there a way to 'save' my PolylineOptions or do I need to save the settings of the PolylineOptions and create the PolylineOptions on startup?
真正的问题是.如何序列化不可序列化的对象?
So the real question is. How do I serialize a non serializeable object?
推荐答案
一个选项是创建PolylineOptions类的可序列化版本.
One option is to create a serializable version of the PolylineOptions class.
例如:
public class Blammy implements Serializable
{
public Blammy(final PolylineOptions polylineOptions)
{
//retrieve all values and store in Blammy class members.
}
public PolylineOptions generatePolylineOptions()
{
PolylineOptions returnValue = new PolylineOptions();
// set all polyline options values.
return returnValue;
}
}
如果PolylineOptions对象不是最终对象,则可以使用Serializable类(一个简单的包装器)对其进行扩展并实现
If the PolylineOptions object is not final, you can extend it with a Serializable class (a simple wrapper) and implement the
private void writeObject(java.io.ObjectOutputStream out)
throws IOException
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;
private void readObjectNoData()
throws ObjectStreamException;
派生类中的方法.
这篇关于在Android中序列化或保存PolylineOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!