How to add multiple numbers in Sharedpreferences and always keep the instance of Sharedpreferences?

Asked

Viewed 95 times

1

I needed to add a data in Sharedpreferences and it will wipe out all the data, not "zero" plus my data. I am using the editor.putStringSet("data", dado); to store my data, I needed to keep this data forever. Is it possible to do this? Thanks in advance!

  • Sharedpreferences serves for that same, persist data. Of course when uninstalling the application they are lost.

  • 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.

  • I don’t know if I understand your problem, but every time put is used, for a certain key, the previous value is replaced.

  • 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.

  • 1

    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.

  • Explain better what you intend to do.

  • 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.

  • 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.

  • vlw by Ramaral help, but I will save this in a bank even, I think it will be even easier to manipulate the data.

Show 4 more comments

1 answer

1


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.

  • 1

    How can this be functional? You use a randomUID for keys. How is it then possible to recover a certain value if we do not know which key was used to store it?.

  • That is why I recommended you to use a bank http://answall.com/questions/81525/inserir-sqlite-em-applies%C3%A7%C3%A3o-android/86802#86802 Any questions I am available to help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.