I'm developing an android game, and on each level, i have the level information written on the code, like these example.
LevelOneballsNumber = 5;
LevelTwoballsNumber = 7;
Level one = new Level(1, LevelOneballsNumber , #FF462F);
Level two = new Level(2, LevelTwoBallsNumber, #FFFFFF);
This approach seems very hardcoding to me. There's some way to store this kind of information (information that never changed), and get them during the game?
you can save the information on file , and get it from there .
or to make a "manager class" or some static class that will contain all the information of all the levels .
So based on the information that you have provided, personally I see two approaches.
Approach 1:
One of things which you could do, is use the sql lite database if you are really concerned about keeping them as unchangable and it will persist and you can read out of there.
Approach 2:
The Other way is to make it a static class and then have reference of it for each user, but I am guessing you would have to implement a multiton design pattern to be able to effectively do it, you could do this but would involve a little more complexity than using the database directly.
I am sure there are more ways to do this, at the end of the day your call, but personally I would go the Database way.
DBs FTW!
Another approach is to store the information in an xml (or json, etc) file as your asset, and then parse it in runtime with your favorite xml / json parser. This allows a more flexibility in the schema than storing it in a relational database, and can be easily edited independent of the code.
Related
In my android application I have to store some application settings and some user information within the phone.
I can go for the shared preference option explained in this DOCUMENTATION.
But wondering if I can store data as objects wise within the phone. I found this Stackoverflow Question regarding saving serialized objects in files and bit not sure of any issues if I go with this way to store persistent data.
Also would like to know what the best way to deal with insert/delete/update and read with XML files in android. Would appreciate any guidance. Thanks in advance...!!!
If it is only a small amount of data you need to store, then go with the built-in shared preferences, that is what the functionality is there for. SQLite and OrmLite are a bit heavyweight in this situation IMO. Even if you want to handle the data as Objects; in which case I would serialise to / deserialise from JSON or XML stored in text files and handle the insert/update/delete on the deserialised objects in your model.
If you want to persist some objects I think you should use SQLiteDatabase, it would be a more cleaner solution than using serialization in files. You will indeed need to write some extra code for your Database but you will end up with a cleaner implementation in my opinion. You could also be using OrmLite for Android which is pretty robust and easy to use if you have some basic orm knowledge.
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!
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
There are 2 ways to do this
Save it in a SQLite database..
Save the objects in a json format in a file
See this discussion
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.
My app needs to store data on the phone, but I'm not sure what's the more efficient method. I don't need to search through the data or anything like that. I just need to be able to save the app's current state when it closes and restore when it's back up. There is between 1mb and 10mb worth of data that will need saving.
There are basically a bunch of custom classes with data in them, and right now I have them as Serializable, and just save each class to a file. Is there any reason for me to change that to store it in SQLite?
If you where to use sqlite you could save as you go, and know that whats in the DB is pretty much uptodate if the app/activity holding the data is suddenly killed by the os. Other that that I cant see and obvious reason to use sqlite for your use-case.
Also for the sql approach you have a clear cut way to change the structure of your domain objects at a later time and to migrate the data from a old to a new version of your database. This can be done using serialized objects as-well, but then the objects needs to be duplicated, both new and old at the same time. And that to me sounds very very scary and messy, especially considering that you might need to go from version x to y, so you might end up with some pretty tricky problems if you ever need to update the domain objects.
And I can honestly not see any benefits of using the flat-file/serialized approach.
You mention in your question that the data is only meant to save the state of the app, therefore my initial response would be to keep it on the devices especially since you mention that the file size would not be much more than 10MB, which is quite reasonable.
So my answer to you would be to keep it as is on the device. If your usage of the information changes in the future, you should then reconsider this approach, but for now it's totally logical.
If you're only saving serialized classes, you could use an ORM mapper as discussed in this thread . This saves you the inconvenience of writing your own mapper and is easily extendable to new classes. Also, if your requirements change, you COULD lookup data.
The only reasons for changing your system to SQLite would be more comfort and maybe a more foolproof system. E.g. now you have to check if the file exists, parse the contents etc. and if you'd use SQLite, you don't have to verify the integrity of the data and Android also helps you a little. And you could use the data for other causes, like displaying them in a ListView.
I want to store structure type data (i.e. information of call logs like name, number, type of number, date, time, duration). Which is the best way and which is faster? SQLiteDatabase (make table and insert, delete logs in it) or use file storage (means make one class for all parameters and write their objects in file using input/output Stream and serializable) or the another way i heard about it is XML parser but i don't know this.
Please help me. Thanks in advance.
It depends on what you are trying to do.
If your goal is speed, the SQLite will give you a serious run for your money (especially if you wrap multiple inserts into transactions). SQLite has been optimized for everything you mentioned and it would be very easy to leverage the speed it can give you.
If portability is your goal, then files may be a slight bit easier. Files can be moved back and forth very easily easily, whereas SQLite might take some more effort.
If being able to search is your goal, then you'd be a fool not to use SQLite, as it is extremely good at searching and filtering results.
I can't give a very informed answer here because I'm just as new to the subject as you are, but here is the link from the developers page that goes over the different types of data storage. I hope you find it useful. http://developer.android.com/guide/topics/data/data-storage.html
Personally, given you know the basics of Databases I would use a sqlite database. It's pretty straight forward in Android. In terms of speed I don't know which is faster, but if you don't have millions of datasets it won't matter.
In my experience in most cases JSON in a file is enough (mostly you need to store an array or an object or just a single number or string). I rarely need SQLite (which needs more time for setting it up and using it).
I am new to android. I am creating ToDoTasks application in android. i have crated Gui of the application and it is working perfectly. I am able to add tasks in it. Now i want to know that i want to save 'task list' in an area so that every time user comes on it , than it should maintain the list of previous tasks which were added in it. What is the best way to do this ?
Whether i should go for database in android or is there any other way to do this ?
Please suggest me. Please don't mind , i know this is a silly question but i have no other way to solve it.....
You have multiple options with varying degree of complexity.
Do you foresee sharing your todolist with another application. If yes then you need to host your data as a content provider.But I digress.
The most simplest option is Shared Preferences. The api is very simple to use and you do not need to write a whole lot of plumbing code. You can directly store an list of string in the shared preference of your activity.
The more elaborate solution is using sqllite. If you foresee your domain model to become more complex than just a list of strings, then you should see if the additional complexity is worth it.
Look here for more details. (I will not worry about the file options, the other two mentioned here are superior to that solution)
You can use sqlLite db for android. See this or this for example.
Here is another example of an SQLite implementation:
http://p-xr.com/android-tutorial-simple-but-persistent-data-storage/