0

I am making a simple app which saves the products to database. But I dont want user to enter the all product data all the time. I want user to define max 5 products to make the saving them easier. I want to save the product size, color, price with sharedpreferences. But when I try to save the new product, old one is being deleted.

Is there a way to save 5 different sp?

turguttak
  • 3
  • 4

1 Answers1

0

Basically check if you're initializing SP correctly and then putting the value and fetching it correctly using sharedpreferences.

Initialization

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();

Storing Data in SharedPreference:

editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string

Retrieving the data from SharePreference:

pref.getString("key_name", null); // getting String
pref.getInt("key_name", -1); // getting Integer

Check this if you want an example on this.

Omkar C.
  • 755
  • 8
  • 21
  • I can save and get data. no problem with it. The problem is I want to save 5 different sp. For example for product A, B and C, I want to save size, color, price for each and I want to get values as " B.price " – turguttak Nov 03 '19 at 22:06
  • Alright have a look at this page and you'll get your answer. https://stackoverflow.com/questions/9054193/how-to-use-sharedpreferences-to-save-more-than-one-values – Omkar C. Nov 03 '19 at 22:13