Regarding shared preferences in android - android

iam taking the data from user and storing in list and now i want to store the multiple data items of the list in shared preferences in android is it possible.

SharedPreferences are not the ideal storage for data lists.
Even if you can do it, you would better consider using a database:
http://developer.android.com/guide/topics/data/data-storage.html#db

Related

Android shopping cart : How to keep products when user exit from app?

I'm developing an e-commerce application like flipkart or amazon , i need to keep the products in the cart even if user accidentally exits from app and iam following tutorial
http://www.androiddom.com/2011/06/android-shopping-cart-tutorial-part-2.html
can anybody tell me how to store products even after exiting from the app ?
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, but for your specific case i would go for Shared Preferences:
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.
To store data you would typically use a Database. Personally I would recommend Realm as a local database for Android given it's simplicity and good documentation
You can use shared preference to store local cart data.
Also, SQLITE db is another option to store users cart data.
You can use a SQLite database.
Here's an official documentation.
You can use a database (SQLite) or use a sharedpreferences key with a string of the selected products
You can use sharedpreferences like so:
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("cartporducts","porduct1;porduct2;porduct3;porduct4;...").apply();

How do I store an ArrayList<HashMap<String, String>> in SharedPreferences?

How can i store an array list of hashmap in shared preferences android?Any help would be appreciated.
After API 11, Shared Preferences accepts sets such as ArrayLists with HashMaps embedded in them.
Please go through this link for a thorough walk-through about saving ArrayLists to Shared Preferences:
Save ArrayList to SharedPreferences
It will solve your problem.
P.S: The sole purpose of Shared Preferences is to save the values that are less frequently accessed and that are not too Large (like the settings of Application). Therefore, if you have Data Sets that are large in sizes and are not related to to the Application's Settings, then you should go for a proper SQLite DB.
EDIT:
Other Options available for storing the Data in ArrayList is to use a proper SQLite Database.
Here is the link that contains code and information related to Storing and Retrieving ArrayLists from SQLite Database.
I hope this solves your problem.
What i usually do is convert the Map to a json-string and save that. Easy enough to change back to a hashmap.
Works like a charm.
Check out the answer from KayKay in this thread for some example code.
In order to store Object type into the SharedPrefrence.
You can refer to below post having similar issue.
Download gson-1.7.1.jar from this link: GsonLibJar
How to store class object in android sharedPreference?

Difference between shared preference and sqlite

I know this topic has been discussed before on Stack Overflow. But there are still some things that are not clear when I read previous posts about it. So here they are:
I know that we use shared preference for small datasets and sqlite for large data manipulation, so if we just want to save a username and password should we use shared preferences?
Won't shared preferences be lost when user uninstalls the app? For example I download an app called abc and save my username and password. Then I uninstall this app from one phone and try to access it from other phone using the same username and password. Will this be saved using shared preferences or the data be lost?
What are the main reason we use one over the other beside large and small datasets?
You can think of the difference between shared preferences and an SQLite database in terms of data size but that isn't entirely accurate. A better way to think of it is in terms of the structure of the data you want to store.
Shared preferences can only store key-value pairings whilst an SQLite database is much more flexible. So shared preferences are particularly useful for storing user preferences, e.g. should the app display notifications etc. Whilst an SQLite database is useful for just about anything.
Both data sources are local but something you should be aware of is the ability to backup your application data to cloud storage that is linked to the user's Google account. This makes it much easier for your users to change devices and for their applications to easily transfer to the new device. For more info take a look here.
In the situation you described about you will lose the user name and password in both situations. The data is stored on the phone, when you uninstall the application, the data that some with it will also be lost. The user will have to re-enter this information.
You can save the user name and pass in either the shared Preferences or a DB, that is personal preference. Just make sure you lock either down, i.e. don't share the DB or Shared Preferences that you keep this information in.
As for the difference... shared Preferences should hold well... shared Preferences... here is an example:
If I create an option to change the background color, I will store all available options in a DB that can be loaded into a adapter view for the user to choose from. But I will store the color that they have selected in the Shared Preferences. This way when the application load I can get the Shared Preference value of the background color that should be used.
SharedPreferences is used for just that, storing user preferences shared application-wide. You can use it, for example, to store a user's username, or perhaps some options he or she has configured in your app in which you want to remember.
SQLite is a relational database. It's used to store your application's data, not preferences or configuration information.
Both are stored locally on the device.
1.SharedPreferences stores only Boolean, int, float, long, String five kinds of simple data types, such as can not be conditional query. So, whether SharedPreferences data storage operation is how simple it can only be a supplement of storage, but can not completely replace other data such as the SQLite database is stored.
2.SharedPreferences based on the XML file to store key-value key used to store configuration information(mainly user preference for your application).
3.Sharedprefrece just like cookies in web which store some basic information at client side.
both store their data locally, so uninstalling the app will delete both. other than that, SharedPreferences is easier to program, and you're right about the data amounts.
In general, shared preferences should be used if you want to allow your user to directly manipulate certain data fields. Shared preferences are basically user preferences; if you would like the user to reconfigure the app to behave in different ways, you should expose that functionality as a shared preference. On the other hand, the SQLite database should be used if you want to limit the visibility of the data to just the application, if you want a stronger guarantee that the data be persistent, and if you want the application to behave independently of what is stored in the database. Of course, you can use both in one application.
Shared preferences and the database are part of local data that the application stores. If you uninstall the application, both of the data stores will be removed.

Android Preferred Storage

We are developing an application in android,We have to save some lists and data to be used by application.We have saved it in sqlite and we use that data by using cursor.
Now my question :
What is more efficient to store data in we should continue with sqlite or to store data on strings.xml or we should use shared preferences ??
Sql Lite is for sharing your data as a datasource. It does not make sense
otherwise as you have the overhead of writing queries to serialize your objects.
Shared Preferences is a preferred way to store internal data.
You can't use assets as said by someone else before because its read only.
Well It Depends on your requirements. If your requirements are dynamic then you should try Sqlite for Storing the Datas.
You should use String.xml to store constant strings, which are not going to change easily ( i mean by increment or decrement of Strings ).
In your scenario, I think best way to store is in asset folder. Means store that data in txt file in key-value pair and while displaying in list, your just have to read that and display in the list. So that saving step can be avoided.
Shared Preferences are generally used to store primitive data. I'd suggest you to go with SQLite .

SQlite or Shared Preferences

In my android app I receive data as json like this:
"_playLists":[{"name":"Playlist 1",
"items":[{"name":"Poza 1","target":"...","url":"...","url_thumb":"..."},
{"name":"Poza 2","target":"http:\/\/audifrance.fr","url":"...","url_thumb":..."}]},
{"name":"Playlist 2",
"items":[{"name":"Poza 3","target":"...","url":null,"url_thumb":null}]}]}
My question is : which is the best way to save data from this response: using Shared Preferences or using a database SQlite? Is it possible using Shared Preferences to make connections between the saved data?
If there is no security concern with data, data is complex type and data is in large amount I think SQLite is the best for it.
And if the data is small, data is premitive type and you don't want to share it with user then SharedPreference is good.
Choice is yours.. Thanks.
both have different purposes .
for small sized primitive data's where you don't want to invoke any queries use shared preferences .
for large sized , complex type storage , use sqLite

Categories

Resources