0

What is the best way to save multiple (about 80) fields as a single entity? My app creates a "program" object which itself contains about 75 Strings in an array, about 4 other random Strings, and a single int field. I want to be able to save these so that the user will be able to view basic information about each program in a ListView, and then click one of them to view the rest of the data.

What is the best way to save these? I've been thinking about saving them each as their own separate XML file, but I've also heard about databases (though though seem a little much for what I'm trying to do). Or should I just save them as regular .txt files and parse them later? Or maybe even just save every single program in to a single txt/xml file, and parse them all together? Or is there something even more efficient that I'm missing?

Michael
  • 3,334
  • 20
  • 27

5 Answers5

2

As long as you're not creating a substantial amount of Objects, I'd say that Serializing your Object and storing it that way would be sufficient. If you find yourself creating a number of these Objects I'd suggest you look into a database implementation to handle them.

shellström
  • 1,417
  • 13
  • 29
  • Yes, this is a very simple way of putting Java data structures into persistent storage without all the faffing around with XML or SQL. – David Given May 28 '12 at 16:22
  • What would you consider "substantial"? The user will be able to make as many as they want, so unfortunately there isn't any way that I can tell. – Michael May 28 '12 at 16:24
  • 2
    Well, in that case Serializing isn't really a fit option as the number of potential Objects is unpredictable. I'd say Serializing is fit when it comes down to a number of about (but don't take this number as any type of fact, it really depends on the type of data and if the number of Objects are limited) 1-10. Anything above 10 I would consider to be substantial. And the fact that the Objects you have contain same type and number of fields makes it much more viable to use an SQLite database or an XML-writer/parser. – shellström May 28 '12 at 17:29
1

It depends:

If you want something fast - use SQLite database.

If you want something easy to implement (but slower) - use SharedPreferences.

Michał Klimczak
  • 12,674
  • 8
  • 66
  • 99
1

In your case store data into a XML file is the best way. SQLite Database can be used for a stronger work.

113408
  • 3,364
  • 6
  • 27
  • 54
  • The way that my data is structured, an XML file really does seem like the best way. Do you have any recommendations for how to go about this, such as a particular XML writer/parser? – Michael May 28 '12 at 16:30
  • This topic would help. there is a lot of way to do your work. http://stackoverflow.com/questions/2290945/writing-xml-on-android – 113408 May 28 '12 at 21:12
0

If you don't want to worry about Cursor or database management just use SharedPreferences. I'm used to serialize/deserialize my objects using GSON, which is easy to setup and works pretty well.

Wizche
  • 893
  • 13
  • 32
0

You could have a look at the ObjectOutputStream class too. It can print an object that implements the Serializable interface to a file or other stream.

So you could do something like making an ArrayList of those Strings and int, and write the ArrayList to a file.

11684
  • 7,356
  • 12
  • 48
  • 71