I am trying to create an app that shows a list of picture(with pagination) from an API, and when you click on a photo, it opens the photo (probably in a new activity) with zooming and stuff. My question is how should I store the API responses? Should I
Store them in Application class
Make a singleton class and store it there
Use a SQLite database
I know the answer will heavily depends on the data, so at least what should I consider when choosing between these 3 options? Or are there better options?
Thanks!
best answer is database
you can store image url and other info easily in a sqlite database
easy store / easy management
It depends on the scope of your data according to your requirements.
If you want your data to stay accessible until app is running, then Singleton Class or Static Array (Create a static Array in some JAVA class let's say named "Helper" which is not being used anywhere, and access you array like "Helper.yourArray" to add or get data) will the best option.
But if you want the data to stay accessible until the app is installed in your device, in that case Sqlite database or Shared Preferences (Convert your JSON into String and store in Shared prefrences and when you need access that String, convert into JSON and parse to use) is the option you are looking for.
Hope it will help you to choose!
Related
On my android app I'm using a custom class, that extends from application, to share persistent data between activities and what I want to know is the best way to store that class ? With this I mean; what is the best storage option for my custom class so, if the user reboots or kills the app, I don't lose any information.
Your options:
Parse the object to a JSON string and save it in shared prefrences. google-gson
Serialize and save the object. example
Use a SingleTon to make data accessible. example
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.
I have the following data tuple describing the user of my application:
(userID, name, email, contact number)
and I want to access this data frequently throughout my application. I have a contacts table in my database in which tuples of this type are stored to describe other contacts. The reason I don't want to store the tuple describing the user is because it seems wasteful to have an attribute to mark which data is "self", and also I use the table directly for inviting users, so I don't want users to see themselves on the list of contacts! I thought about using shared preferences to store "self", but I may decide to include more data in the future, and piecing together a tuple from several key:value lookups seems like a very messy solution. What's the best way to store these "shards" of data so they're easily accessible and not wasteful?
Thank you :)
try Shared Preferences
http://developer.android.com/guide/topics/data/data-storage.html#pref
Here are your choices for storing data:
Android Data Storage
I would try using the external file if you don't want to use shared preferences or a database. XML and JSON files are both good choices that can be easily parsed. If you try a JSON file you could access it using GSON in your code.
Another option is to create a subclass of Application which has fields (and get/set methods) for your variables. Use this class in your <application> tag in the manifest. Then use e.g. ((MyApplication)getApplicationContext()).getUserId()
If the fields are static you might find they persist across application instances. I haven't tried this though.
I am working on a project in which I am saving data in a class with a filename and data attached to a folder on the sdcard. I need another class to display a list of all the filenames and when you click on them it opens the saved data (text) in an editor or something. I am somewhat new to android, sorry if I am missing something obvious here.
So basically I need to display the file names of data that I have saved in a list view and when you click on them, it takes the data and sends it (Probably using shared prefs or BroadcastReceiver) to a class where you can edit it. I could also use a database if that would be easier.
See First way is to make variable as static so you can access by class name.
second Use file for data storage it is easy to create file.
third is use Share preference i.e.mobile seeting
fourth you can use SQlite to store data.
There are different ways to store the required data:
Shared Preference (If the data is lesser say for example login
creadentials etc).
Internal\External Storage (we can store data images,files etc .Your sd card case is of external storage)
Database (SQL Lite is present and stores structured data in a private database.)
Study the link for storage options.Choose the best options as per requirement and need.
http://developer.android.com/guide/topics/data/data-storage.html
Actually i want to know how to store data from my app in the device so that i can review the store data when i run the application again..
means in simple terms i want to say that suppose i have text box where i write some information..now when i click the submit button, this information will be save,so that when i open the application the stored data should be appear in the text box..
In all terms i want to say that i just want to stored data in the way that we are using database for storing data..so please anyone suggest me how that can be done in android.
if possible show with an example
Regards
Anshuman
If you have to store small amount of data, you can use SharedPreferences in Android.
If the data that you have to store is big/complex enough, try using SQLite database.
Still need help?
UPDATE: There's a tutorial that I wrote to demonstrate how to use SQLite database. check it out here. Although it copies existing database into device's memory, but other versions of it, which create database through code can also be devised from it.
A better tutorial is here : http://www.vogella.com/tutorials/AndroidSQLite/article.html
1) If you want to store data in table format then you can use SQLite database in android
2) If you don't want to store data in table format then you can store in SharedPreference
more info about SharedPreference here and here
Android comes with a built in SQLite database that you can use. I advice you to go trough this notepad tutorial. It teaches the basics of using Android SDK including different states of the android application as well as how to use SQLite with Android.
For storing simple key = value pairs, you can use Properties.
For data storage as in a database, you can use sqlite on android.
Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires.
Your data storage options are the following:
Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
External Storage
Store public data on the shared external storage.
SQLite Databases
Store structured data in a private database.
Network Connection
Store data on the web with your own network server.
Data Storage