简体   繁体   中英

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. My class assignment only has some primitive variables. My MainActivity has an ArrayList of courses. 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.

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.

Then you can load the JSObject into you application and make changes without having a fixed structure.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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