Argus9
Argus9

Reputation: 1955

Best Way to Store Custom Objects to Internal Storage in Android?

I have a custom object, "TimeSheet", which itself contains Calendar, DateFormat, and int fields. The App is designed to use several of these objects, so I'm planning on storing them in a List as they're created and I'd like the App to be able to save these objects to internal storage when the App closes and reload them when it opens.

I'm still something of a novice when it comes to Android development (I've only published one App so far), so I'm not entirely sure of the best way to go about this. I'm guessing an ObjectInputStream and its Output counterpart are probably the best options, but I'm not entirely sure. I'm completely willing to change my design strategy to store a collection of these TimeSheet objects in the easiest way possible.

Can anyone recommend a good direction to go from here, and if possible, provide brief, simple examples?

Thanks!

Upvotes: 4

Views: 4043

Answers (3)

Sagar
Sagar

Reputation: 41

There are 2 ways to do this

  1. Save it in a SQLite database..
  2. Save the objects in a json format in a file

See this discussion

Upvotes: 4

BlackHatSamurai
BlackHatSamurai

Reputation: 23493

There is no single right answer for something like this. A lot of it depends on the amount of data that you are storing. If you don't have much data, used SharedPreferences, if you have lots of data and it is complex, use a database. I wouldn't use a database if you don't have much data. You want to keep things as simple as possible and adding a database can complicate things. Here is a link that talks about the different options. Check it out. Hope it helps:

http://developer.android.com/guide/topics/data/data-storage.html

Upvotes: 6

Femi
Femi

Reputation: 64700

I'd honestly recommend using a SQLiteDatabase to store them: write functions to map your 3 fields to the database (Calendar would become a NUMERIC, DateFormat would be a String, and the int fields would all be NUMERICs) and to rebuild your object fields from a row in the database. Its a bit heavy up front but will make the inevitable feature expansion much easier.

Upvotes: 0

Related Questions