Android: Can I save more than one SharedPreference entry to a file?
This is the problem: I have a form for inputting names, then saves it, then displays it in another activity. But once 开发者_如何转开发I try to enter another name, the previous name is overwritten by the new name.
Is there a way to set these names up to list themselves one after another without overwriting each other in SharedPreferences?
You can as long as they have distinct names. Ig you need multiple values for same name, you can store JSON array or use some form of prefix / suffix solution to provide unique names
Either do it like Konstantin Pribluda suggested, or you might think of using the SQLite, if you have a lot of names you want to store (e.g. if creating a history of typed in names). That way you can store unlimited values for the same key and retrieve them as a list/cursor. But of course that's overkill if you only have 2-3 names…
you can also save a Set of Strings in the SharedPreferences.
精彩评论