To keep the old data you must enter a new key.
Here is an example of generating a GUID-based key, where this key will never be repeated.
Import: import java.util.UUID;
and do the following:
UUID uuid = UUID.randomUUID();
String randomUUIDString = uuid.toString();
editor.putStringSet(randomUUIDString , dado);
I hope I’ve helped.
But I recommend you create a database SQLITE
, so you will have a control over your information and can do searches and change the data quickly and easily.
See my answer to this question Insert sqlite in android app . This will definitely help you implement a bank SQLITE
on android.
Sharedpreferences serves for that same, persist data. Of course when uninstalling the application they are lost.
– ramaral
Yes, Sharedpreferences saves everything in an xml that you only lose if you uninstall, the problem is that when I create Sharedpreferences I am creating a new list and putting new data, I needed to keep the data, I will try to create Sharedpreferences on onSaveInstanceState to see if it is right.
– daniel12345smith
I don’t know if I understand your problem, but every time
put
is used, for a certain key, the previous value is replaced.– ramaral
exactly, because I recreate sharedPreferences, I needed to keep it, and every time I save my Activity, save my data by keeping the previous data.
– daniel12345smith
This is only possible if you change the key: first time -
editor.putStringSet("data1", dado);
second time -editor.putStringSet("data2", dado);
etc. But this is complicated to manage, the best would be to use a DB.– ramaral
Explain better what you intend to do.
– ramaral
It’s true, there’s this same key problem. I needed to store all the texts of a Listview that I clicked on a Stringset. When I click on a Listview item I change the color of the item, hence I needed this data for when creating Activity to use this data to put the color in the items that were clicked. In this case onSaveInstanceState would not work, I want to avoid using BD, Sharedpreferences seemed a good option.
– daniel12345smith
If I understand well what you want is to save the status (clicked/not clicked) of the list items so that when opening again the Activity that state be restored. If so, Sharedpreferences is a good option. Enter the code you have already done so I can give an answer.
– ramaral
vlw by Ramaral help, but I will save this in a bank even, I think it will be even easier to manipulate the data.
– daniel12345smith