简体   繁体   English

在Android上存储和还原非原始数据的最佳方法?

[英]Best way to store and restore non-primitive data on Android?

I have written my first app which can do the following: You can store courses and in these courses you can store assignments of the courses. 我写了我的第一个应用程序,它可以执行以下操作:您可以存储课程,并且在这些课程中可以存储课程的作业。 They are both represented in lists. 它们都在列表中表示。

My class course has some primitive variables and an ArrayList of assignments. 我的课程有一些原始变量和一个ArrayList作业。 My class assignment only has some primitive variables. 我的课分配只有一些原始变量。 My MainActivity has an ArrayList of courses. 我的MainActivity有一个ArrayList课程。 Course and Assignment implements Serializable 课程和作业实施可序列化

I am currently using this method: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal by serializing the whole ArrayList into one file in the internal data. 我目前正在使用这种方法: http : //developer.android.com/guide/topics/data/data-storage.html#filesInternal通过将整个ArrayList序列化为内部数据中的一个文件来实现。

The problem which appears now is, that if i want to change the class "course" and add a variable and use the new app, the app wont load my old data since the stored object "course" is not compatible to the new class "course". 现在出现的问题是,如果我想更改“课程”类并添加变量并使用新的应用程序,则该应用程序将不会加载我的旧数据,因为存储的对象“课程”与新类“不兼容”课程”。

How can I store data without having the above described problem? 没有上述问题,如何存储数据?

Store your data as a JSON object in the file. 将数据作为JSON对象存储在文件中。

Then you can load the JSObject into you application and make changes without having a fixed structure. 然后,您可以将JSObject加载到应用程序中并进行更改,而无需使用固定的结构。

You can then read the values in with default calls so that the old code will still work. 然后,您可以使用默认调用读取值,以便旧代码仍然可以使用。

http://developer.android.com/reference/org/json/package-summary.html http://developer.android.com/reference/org/json/package-summary.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM