Android Development - Data storage
I want to create an application where there is a button and each time this button is clicked that some random preset message is displayed on the screen. How should I go about storing these preset messages? Should I use an SQLite database or should I just create an array populated with the messa开发者_如何学运维ges and retrieve the messages from there?
I am not looking for code. I just would like some hints, suggestions, or someone to point me to some documentation that might help me.
Oh, my first instinct was to start pumping out some code, but since you want to just look at some different options, here are the ones that I can think of
Array: Hard code an Array
of all your preset messages and then just make a Toast message with presetString[RandomNumber]. Nothing too fancy. Pretty much straight forward Java with an added toast message. (Information on toast messages and other options for displaying the message can be found here: http://developer.android.com/guide/topics/ui/notifiers/index.html)
Advantage: simplicity. Its very easy to look at the code and see what your goal is. Disadvantage: It is cumbersome to deal with the array for really long lists and future updates can become burdensome. Also it would be difficult for you to use this in any other part of your program efficiently.
strings.xml: http://developer.android.com/guide/topics/resources/string-resource.html Strings.xml is the more "Android Developer" ish approach in my opinion. You make one with all your possible messages and then load it into your application via the resource name. From there it is simply an array that you can perform the same operations you normally would. Advantage: since its in a centralized location with a resource identifier, any part of your program can use it. Has the same base functionality as an array Disadvantage: .apk files are signed, so because of that, your resources cannot be modified at runtime. The messages you put in here cannot be added to or changed
Other storage types such as JSON are available. These would be similar to the strings.xml except for on more plus side; they are easily loaded from a remote server. Your application could make a remote call to request the full array and parse it from there http://damonsk.com/2010/01/jsonarray-httpclient-android/
Finally, you COULD do a SQLLite database. Using them would require a great amount of overhead in there for such a simple seeming application. This would be preferred if you are looking for a persistent solution (one that will keep the data from use to use) AND is changeable. Say for example you would like to give the user the ability to add new entries, or maybe let the user change the messages or delete the ones that they don't like, then this is the appropriate solution. The main concern is of course, is it really necessary? But if it is, then you can find more documentation on it here or here (shows you how to copy a SQLLIte db Be warned though that on Android 2.2 there is a bug that would keep you from adding files that are above 1megabyte in size as an asset, so if you REALLY had lots of messages to add, you would need to use this work around here
If the user can create new messages then using a database makes sense, but if the messages come with the .apk file then just include them as a resource, storing them as .json or .xml files.
It also depends on if you need to be able to pick messages depending on the time of day or location, then you may need multiple files or use a database.
If you used a database you would either pull the information from files, or from a url, to populate it, so, since the data is already in a file with the .apk file don't duplicate it if you don't need to.
精彩评论