I want to build an android application which has static content. I dont know where should I keep my static content. Shall I keep it in my xml file or use sqlite db. Or shall I keep my data in xml file and create the table when the application loads . Data wont change much over the period of time and only fetch operation has to be performed on the data.
It depends on the data which you want to store..If it is some small key value king of data use
SharedPreferences otherwise you can use sqlite database if you relational data..Files are generally used to store non-relational data.
Refer http://developer.android.com/training/basics/data-storage/index.html. to learn how to implement each of these techniques.
Personally, I'd opt for an XML file structure. The data handling code will be comparable but updates from the host database will be infinitely more efficient. Good luck with your project.
Related
I'm developing a little Android app which is going to be offline, and I need to store the SQLite base's data into a file, that I want to put into the apk's resources, I could use XML or JSON files, but I was wondering if there weren't a better solution.
Thanks in advance :)
If I understand you right you have some prepared data to include in app to use.
If you don't want to parse xml, json or raw file every time you need it in the app you could:
1. Parse the file and put contents into SQLite database on the first run of the app.
Then you can use SQLite to query for data.
You can check if this is first run by checking boolean flag in SharedPreferences upon start of application (in onCreate of your main activity or in Application class)
2. If data is not very big you could parse it once and keep it in memory in your custom data structure.
You can create a singleton to query for this data and parse it in case it is not loaded yet.
I'm working on an Android app for homework management. I'm a senior in college, so my experience on larger projects is very limited, but I'd like to design all parts of this app well instead of just throwing something together. This includes the way data is stored.
We have two main objects/entities: Task and Subject. Even if someone uses the app for the whole time they're in college and never deletes anything, I'm guessing there would be a maximum of a few thousand tasks and a couple hundred subjects (not all subjects would be shown at once). The initial version of the app won't sync data with a server, but this is a definite possibility in the future, so I'd like to design with that in mind. We might also have an option for users to send tasks to each other.
Here are my questions:
Would a SQLite database be the best option for storing the amount of data we're likely to have, or would something like serializing it to XML or JSON then loading it into memory when the app starts work?
I'm used to thinking in terms of objects. This means that if I use a database and it has a Task table and a Subject table, my first instinct is to convert each database table row into a corresponding object for viewing/editing. (The objects' setters would contain validation logic.) Is this a good/helpful/necessary way to think? If not, what is the alternative?
Thanks for your help!
This question is broad so may comments below may not be 100% correct as I don't have all the information about your system.
SQLite is better suited for storing thousands of records than files (be it JSON or XML). This is especially true if your data is not static, i.e. will be changed during the usage of your app (which is the case for you, I believe). You can take advantage of existing functionality for records inserts, updates, deletions, using indexes, etc.
Yes, you generally create objects similar to your database. But you don't usually need to convert each and every record from the database into your objects. You usually query the database for a limited number of objects, depending on what you want to show in the UI. Of course, if you need to show all, let's say, tasks, you need to get them all.
1. Would a SQLite database be the best option for storing the amount of data we're likely to have, or would something like serializing it to XML or JSON then loading it into memory when the app starts work?
Yes SQlite will be the option for you.It will give you a structured format and in future if you want to access data from remote end the same structure of tables can be used without much change in the code.
2. I'm used to thinking in terms of objects. This means that if I use a database and it has a Task table and a Subject table, my first instinct is to convert each database table row into a corresponding object for viewing/editing. (The objects' setters would contain validation logic.) Is this a good/helpful/necessary way to think? If not, what is the alternative?
you can simply execute queries to manipulate data.
But dont forget to encryt your database if you storing it in mobile itself.
I am developing a places of interest app which will display the list of places of interest in a location.
When user chooses one, it will display more information and address etc.
How do I store all this data? Currently I am using a text file to store all the data and subsequently when user chooses a place, it will parse the text file and retrieve the necessary data for display.
Any advice on what is a better way to do this? I looked at SharedPrefs, but it is more like storing "key-value" pair and in this case I need to store a large amount of data.
I want the info to be available even when the device is offline, thus I can't download from an online server upon request.
Any other way to do this?
You may store it to XML file using XML serializer, here is very good tutorial for learning that,
http://www.ibm.com/developerworks/library/x-android/
and it can be easily parsed using Java XPath Api. Have a look at this at parsing XML files
http://www.ibm.com/developerworks/library/x-javaxpathapi/
Use SQLite
It can store large data.
It is available offline.
All your problems will be sorted out.
Hre we have a wonderful tutorial for sq-lite
http://www.vogella.com/articles/AndroidSQLite/article.html
How about a relational database?
http://developer.android.com/training/basics/data-storage/databases.html
Take a look at Serialization. If you do not need database access, you could define a class what holds every information you need. Then, you can do the following:
when you need to save the datas, you serialize your object, dumping its content to a file, for example on the SD card
when you want to load the datas, you just load the above mentioned file, and get back everything from the dumped file
I am using this method in my app to cache some datas that would need internet access, so the user can still view it, and with proper implementation, this can work very nicely.
Use database, create table and insert all the data in it. When you need the data just fire the query, and you are done.
SQLite is fine for Android.
Depending on the type of data you want to store, you could use a SQLite Database (provided with Android) if it has a normal database structure. You could Serialize your data and save it in a raw or encrypted file, making you data implement Serializable.
In terms of storing data in Android, would it be more efficient to use a large ArrayList or setup an SQLite database? I have ~9000 bus stops and stop names (both in String) that I need to store and I was wondering which method would be the quickest or most efficient.
An ArrayList is probably a very bad idea. I assume you want the data to be persistent, so if your user kills your app your data will be lost if you use an ArrayList. A database is persistent(unless the user clears the cache of the app). So I would highly recommend using a database over an ArrayList.
If your data does not change then you could probably have it read on startup and kept in memory while the App runs. For example, having a .json file with all the stops, read it on startup, put the data in a HashMap or something that is easy to use since you will probably lookup stuff. It would probably work fine with ~9000 entries.
If you are going to add or update information about the stops during usage then the SQLite is the better choice though.
1.) Store and retrieve your data from a SQLite DB since you want persistance. And since you say you have 9k+ rows a simple select will give you everything at once and you can easily filter the data as well if you need to
2.) At startup, put all your data into efficient memory structures like HashMaps(or in your case arraylists) and reference them throughout the app. This way you'll only do one time access.
3.) When in doubt build a DB. No harm, more efficient and easier to handle than files
I have one Application which has more then 5 Activity I want to know how much space is needed to store 10 different values to SharedPreference. or I have to user Sqlitequery ?
Simple in One Activity I have 4 Field like FirstName,LastName, Age, City when user Click on th button then new Activity is called and in this activity Full Information is Display, so I can do that in two way. First Way I Store all the Information in the SharedPreference and in Second Activity I get this Stored Values and another way is i only store record id and store it in intent.putextra and get it to another activity and Fire Sqlite Query. so I want to know which one is best for Application .(As per Memory Uses and Speed )
If you do not need persistent storage then you can simply pass the data using intents from one activity to another or maybe even keep some static variables in a separate class say Const.java
If you need persistent storage and if your data is limited, I would always recommend using SP over SQLite which should be used for relational data. DB operations take more time and can hit performance besides there is always chance of DB getting corrupted.
Sqllite is a really good solution if you want to be able to add easily more preferences.
It's a bit heavy to set-up but really powerfull.
http://developer.android.com/guide/topics/data/data-storage.html
For me, database is for storing data, and sharedpreference just app config.
Shared preferenses apereas to be the choise here, it is very good to store simple settings and very fast and flexible.
A database would IMO be overkill here :)