使用parcelable将项目存储为共享首选项? [英] Use parcelable to store item as sharedpreferences?

查看:175
本文介绍了使用parcelable将项目存储为共享首选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些对象,位置,在我的应用程序中存储在ArrayList中,并使用parcelable在活动之间移动它们。该对象的代码如下所示:

I have a couple objects, Location, in my app stored in an ArrayList and use parcelable to move these between activities. The code for the object looks like this:

public class Location implements Parcelable{

private double latitude, longitude;
private int sensors = 1;
private boolean day;
private int cloudiness;

/*
Måste ha samma ordning som writeToParcel för att kunna återskapa objektet.
 */
public Location(Parcel in){
    this.latitude = in.readDouble();
    this.longitude = in.readDouble();
    this.sensors = in.readInt();
}

public Location(double latitude, double longitude){
    super();
    this.latitude = latitude;
    this.longitude = longitude;
}

public void addSensors(){
    sensors++;
}


public void addSensors(int i){
    sensors = sensors + i;
}

+ Some getters and setters.

现在我需要更永久地存储这些对象。我在某处读到了我可以序列化对象并另存为sharedPreferences。我是否必须实现序列化,或者我可以使用parcelable做类似的事情吗?

Now I am in need of storing these objects more permanently. I read somewhere that I can serialize the objects and save as sharedPreferences. Do I have to implement serializeable aswell or can I do something similar with parcelable?

推荐答案

来自包裹文件


包裹不是通用序列化机制。此类(以及用于将任意对象放入包中的相应Parcelable API)被设计为高性能IPC传输。因此,将任何Parcel数据放入持久存储中是不合适的:Parcel中任何数据的基础实现的更改都可能导致旧数据不可读。

Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

这篇关于使用parcelable将项目存储为共享首选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆