store data without using database in android - android

i have to make an android application in which i need to download a lot of data from the server which is sent to me via XML. i then need to parse the XML and then display the extracted information.
To avoid making the application slow, i have decided to break my XML down into small parts.. so that i can only call the part that i want, this would limit the information that i am receiving.
My question is once that i have parsed the XML data where do i store it ( except for a db ) until my UI is rendered? On the iPhone there is something called user default where in we can store such information. What would be the equivalent in android?
thank you in advance.

Every thing you need should be right here: http://developer.android.com/guide/topics/data/data-storage.html. Internal storage is the closest to the iPhone equivalent.

You can use application preferences to store data as shown here (if the data is small enough).
Their code sample shows:
SharedPreferences gameSettings = getSharedPreferences("MyGamePreferences", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = gameSettings.edit();
prefEditor.putString("UserName", "Guest123");
prefEditor.commit();
I wouldn't do this for large datasets but it's a handy place to store data.
This gets removed when the application is removed too.
If you've got a lot of data, I'd suggest storing it on disk.

You could store it in a file.
You could store it in SharedPreferences
You could use a ContentProvider --> not recommended for temporary storage.
You could use a SQLiteDB --> I know you said you do not want to use this.

Related

Save SharedPreferences file for backup purposes

I want to save user progress in my game which is essentially all saved in a sharedprefrences file.
So, instead of using third party services, I am wondering if I can simply save the whole file on my server
and in time just pull it and apply it locally?
So the question is - can a sharedprefrences file be saved as a bulk?
for saving progress locally you can use SQLite database or Room Database so you can easily update in the future if you want to update ... I will recommend room database rather than sqlite3 because that is consuming more time and room database easily you can work on crud operation without taking too much time ...and sharedpreference for getting issue if you want to update value and more time to take your job and confusion so use room database...easy and less time
Usually, you can find SharedPreferences xml file in /data/data/<your.package.name>/shared_prefs/ directory. But it depends on manufacter, so it's not guaranteed.
(For more information see this answer).
For getting shared pref file Context has folowing methods - new File getSharedPreferencesPath(String name); and depercated File getSharedPrefsFile(String name);
(According to source )
You can use one of them(or both for different Android versions) via reflection.
But, actually, it's not reccomended approach. Better choice is write your own prefs synchronization by copying all values with all keys.
Or just store preferences in your own database.

Is it a good idea to store multiple values as SharedPreference?

In my android app I have about 100 places (maximum will be 200). I want to allow the user to mark each place as visited and store this selection.
So the user can mark/unmark that he already visited some places/cities.
Is it a good idea if I store the values as SharedPreference?
My code:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("London", "1");
editor.commit();
and next time when user marks another place then:
editor.putString("Paris", "1");
I am asking due to amount of possible places to be stored there, which will be maximum 200 in my case. I am usually using this kind of storage just to store some settings or so, but I think this is an easy way to store the values in my case too, I don't want to use database or anything similar for storage.
Whether this is a good idea or not depends on your requirements and expectations. If you store the data like this, it will work for sure, but, there will be some limitations:
It might be complicated to show a list of places to the user. If you store some other data to shared preferences you will need a way to distinguish places from other data. In this case you'll probably need to add a prefix to all your keys, such as "place_London", "place_Paris", etc.
You are relying on English key names so you might have issues with localization if you support other languages
It will be much harder to support versioning and scalability. E.g. if later you have an entity called "Place" and it has more information than just a name with a flag, then it will be much harder to keep it in shared preferences. E.g. if at some point you want to add a corresponding country name to all places, what do you do?
I think in this scenario you actually DO want to use database. It will pay off.
SharedPreferences is a key/value way to save data. I think it is not appropriate to save large amount of structured data as you have to define a key for each value you have.
Using SQLite might be a better option for your case.
You should switch to a more reliable way of storing data in storage instead of using SharedPreferences.
Sqlite is good option but if you don't like to write SQL Queries and want a solution where you want to store data in your storage, Realm is an amazing alternative.
Although before implementing please read more on the pros and cons of using realm. You can read more about Realm here :-
realm.io
Realm vs Room vs ObjectBox

SharedPreferences or SQLite to store predefined editable information?

I need to store some config predefined information in my Android app such as the list of the servers, logins and passwords. This information should be stored permanently and be editable. For some reason, I think SharedPreferences isn't what I'm looking for. Maybe using internal Android SQLite db would be better?
What do you think?
You can store predefined data in a .db file under R.raw and copy it to your app's working directory then use it with SQLite.
You can specify these setting values in program as variables and insert in sqlite on first launch of application, By this way you can edit these values later in sqlite, I hope this can help you.
I have generally found SharedPreferences to be more easier to implement (also demands less resources than any kind of database). In your case, in my opinion, you should go for SharedPrefences, by loading your required information on first launch through some predefined keys, example, URL + i, where i can be run from zero to the number of URLs. This can be then later retrieved and edited. You can save the initial values in arrays as suggested by others above.

Saving my Fetched JSON data on the android phone

My android app is a news app that fetches JSON data as its content from a link.... I want the user to be able to store these contents on their android phone app directory for future viewing... Please how can I do this?
I'm not 100% sure if this is what you are asking, but it seems you want to store data in your app to the phone for quick access?
There are a few ways of doing this:
Shared Preferences
This is more for settings saved in the app in a key value pair style. You can save just about anything, though I wouldn't suggest it for long blocks of text as its main usage seems to be for small variable persistence.
Internal Storage
This is good for file storage. Probably a little closer to what you are looking for though it may be a little too intensive for something as storing blocks of text from news articles.
External Storage
Much the same as internal storage though through the SD card. This is also a file storage system.
SQLite DB
This would store all the data to a sqliteDB on the phone. This might be closer to what you are looking for if you have a bunch of articles to store and quickly access. This method might require some more work than the others but will probably be a better method in the long run.
Network
This is what you are doing to pull the data and probably not what you want to use to store it if you want to store it locally, but for others reading this. This would allow you to pull and push data from the net.
Here is a link to all of the Android storage options.
You can use SharedPreferences http://developer.android.com/reference/android/content/SharedPreferences.Editor.html
And make you json object implementing Parcelable
See How to use SharedPreferences in Android to store, fetch and edit values
Store data
SharedPreferences prefs = getSharedPreferences("DATA_FILE", 0);
SharedPreferences.Editor prefsE = prefs.edit();
String data = "some data I want to store on the phone for future use.";
prefsE.putString("NAMETOSAVEDATAUNDER", data);
prefsE.commit();
Get stored data
SharedPreferences prefs = getSharedPreferences("DATA_FILE", 0);
String data = prefs.getString("NAMETOSAVEDATAUNDER", null);

What is the best way to store string? SharedPreferences Set Collection or Files?

I have to store some data (string) in my Android app and I'm a dilemma. What is better solution ? Use Set Collection and keep it at SharedPreferences or I should save data to File and when I need it I have to read data from file and put it for example on ArrayList.
Depends on the quantity & complexity of the data. There is no straight answer to your question.
General approach: If the data are not too sensitive, small in quantity and more frequently used then you should go with SharedPreferences.
If your data is quite large lets say few hundred KBs of String then you should go with File.
SharedPreferences ultimately stores data into a file. The advantage of using SharedPreferences is that, the data is stored as a key value pair and can be retrieved easily using a key.
It depends on what you want to store.
Use SharedPreferences when you want store simple, prmitive data. Keep in mind that SharedPreferences will be available only for your app, so anothers apps cannot get data from it.
Use File when you have more complex data. You have to take care about how file is available to others. If you put it in sdcard root directory for example it will be available for everyone. If you put it in app package it will behave like SharedPreferences.

Categories

Resources