Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to stored a data with sharedPreferences in Kotlin

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my android app with kotlin, The product have as a parameter "id", "name","image","color","quantity","price".
In my app, i create an interface that contains a list of all products, and at the bottom of this interface a button "add to bag", i choose the product then i click on add to bag, as a result, the product will be added to my bag.
I want to know how can i stored all the clicked product in shared preferences?
the following code is a function that i created, i want to know if is it true or not
 
 
Saloon Keeper
Posts: 5710
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it work?

Don't forget to call apply().

One thing to note is that shared preferences aren't meant to store lots of data, maybe a 1000 characters as a rule of thumb per item. So if the array is even medium-sized, you should use a different storage type.
 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried for a one product it work, my problem is how to stock a lot of products when i choose a several products, i'm really stuck
 
Tim Moores
Saloon Keeper
Posts: 5710
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuck with what? The shared prefs don't care what's in the String you're storing, whether the JSON represents one product or several.

But as I said before, if it's a lot of data, shared prefs aren't appropriate. Consider the built-in DB or a file in the private storage area instead.
 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Stuck with what? The shared prefs don't care what's in the String you're storing, whether the JSON represents one product or several.


As you said i can stock in the same key a lot of Product ??

       
 
Tim Moores
Saloon Keeper
Posts: 5710
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand how this code relates to the code you posted earlier.

Here's what I understand: 1) You have no problem storing a single product. 2) You do have a problem storing more than 1 product. Is that correct? If so, and assuming that you're storing the data in a JSON format (as the earlier code implies), how can it possibly matter whether that JSON represents one or more products?
 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the code contains "productBag" the product clicked and the parametre of it, and the function "setDataByKeyValue" is created in Utility class, "Products" is the Key and "productBag" is the value.


Here's what I understand: 1) You have no problem storing a single product. 2) You do have a problem storing more than 1 product. Is that correct? If so, and assuming that you're storing the data in a JSON format (as the earlier code implies), how can it possibly matter whether that JSON represents one or more products?



That's what ii want thank you
 
Tim Moores
Saloon Keeper
Posts: 5710
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So your problem is solved?
 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope that,
i will tell you if i have a problem
 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, still have a problem
The product will be overwritten if I choose another product, and only the last product is appear
 
Tim Moores
Saloon Keeper
Posts: 5710
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there code that would read the stored list and update it as appropriate? If so, post it here. If not, write it :-)
 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the following code contains two function, the first one to set the data by key value and the second to get the value  
 
Tim Moores
Saloon Keeper
Posts: 5710
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So which part do you think should take an existing list, add or remove items from it, and then store the list?

If you think that no such code exists, then take a stab at writing it.
 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following one, no ?
 
Tim Moores
Saloon Keeper
Posts: 5710
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code retrieves a list from the preferences, adds the new ones if there are any, and then stores it back as a preference?
 
Marshal
Posts: 65050
247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP: Don't edit posts like that. You have made the answer incorrect. Pleasee post yoiur question anew.
 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about the two function as following
 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:OP: Don't edit posts like that. You have made the answer incorrect. Pleasee post yoiur question anew.


I'm sorry, it was a typo
 
Tim Moores
Saloon Keeper
Posts: 5710
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what about the two function as following


They look like they would read and store a list as a string preference via JSON. Whether they work is easy for you to test.

But I thought the problem you had was this:

The product will be overwritten if I choose another product, and only the last product is appear



In other words, some sort of updating needs to happen - reading a list, comparing it with what the user now selected, making appropriate changes to the list, and then saving it. These two functions perform the 1st and 4th step - do you have any code that would carry out the 2nd and 3rd step?
 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please see what i doing, first of all in my activity, i create a variable
   
an empty arrayList of type "ProductData".
Then, evrey time when i click on bagButton, i added the chosen product to productBagChosen and i added to sharedPreferences.
The following code is the activity:



But still have the same problem,  The product will be overwritten if I choose another product, and only the last product is appear

 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my final code, the list of chosen product appear finally,

But still have a problem, for the first run of the app an error as the following appear :

i don't know why,despite that json is string? type.
How can i correct this problem?
Capture-du-2019-07-31-12-41-24.png
[Thumbnail for Capture-du-2019-07-31-12-41-24.png]
 
Maha Sakka
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help please
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!