开发者

Android: Can I save more than one SharedPreference entry to a file?

开发者 https://www.devze.com 2023-04-12 19:24 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号