问题
I'm creating an app which has several users. I've created an object (user), which holds more details (name, age, custom objects etc). Because I have a custom object, I know it'll be difficult to use shared preferences. What would be the best way to store the users data? I've looked into SQLite, but I can't find a way to save custom objects into SQLite?
Any advice would be appreciated.
回答1:
You can store objects in using any of the ORM libraries, one I would suggest is ORMLite
Use SQlite with ORM
ORMLite has a Android backend which makes calls to the native Android OS database APIs to support its ORM functionality. A number of Android developers that are using the framework successfully. See here for more information:
http://ormlite.com/sqlite_java_android_orm.shtml
In terms of many-to-many relationships, ORMLite does not support cascading or any of the more advanced ORM features but there are examples of easy many-to-many implementations:
http://ormlite.com/docs/examples
Use db4o with ORM
Instead of storing them in SQLite you may store them in db4o, which is specifically designed for storing objects. A simple tutorial for sotring objects using db4o.
回答2:
Look at Externalizable. Here's a blog article about it: Externalizable
This deals mostly with making your custom object externalizable. I suspect there's a way to write these to shared preferences; I usually just send them to a file. You could make an array of user data and stream the whole thing to a file.
来源:https://stackoverflow.com/questions/14471187/best-way-to-save-user-profile-details-android-app