Java 使用parcelable 将项目存储为共享首选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28439003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:13:51  来源:igfitidea点击:

Use parcelable to store item as sharedpreferences?

javaandroidstorageserializable

提问by xsiand

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:

我的应用程序中有几个对象 Location,存储在 ArrayList 中,并使用 Parcelable 在活动之间移动这些对象。该对象的代码如下所示:

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.

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 做类似的事情吗?

采纳答案by StenSoft

From documentation of 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.

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

回答by Cristan

Since parcelable doesn't help to place your data in persistent storage (see StenSoft's answer), you can use gson to persist your Location instead:

由于 Parcelable 无助于将您的数据放置在持久存储中(请参阅 StenSoft 的回答),您可以使用 gson 来保存您的位置:

Saving a Location:

保存位置:

val json = Gson().toJson(location)
sharedPreferences.edit().putString("location", json).apply()

Retrieving a Location:

检索位置:

val json = sharedPreferences.getString("location", null)
return Gson().fromJson(json, Location::class.java)

In case you're still using Java, replace valwith String, Gson()with new Gson(), ::class.javawith .classand end each line with a semicolumn.

如果你还在使用Java,更换valStringGson()new Gson()::class.java.class而结束与semicolumn每一行。

回答by LowFieldTheory

The preferred way would be to implement an IntentServiceprobably

首选的办法是实施IntentService可能