【问题标题】:Store and get data with SharedPreferences使用 SharedPreferences 存储和获取数据
【发布时间】:2011-11-02 16:55:28
【问题描述】:

我想从 string.xml 中存储和调用用户最喜欢的链接,我该怎么做?

我想澄清一下,用户只为最喜欢的 url 输入一个名称,例如,体育它将与用户当前在我的应用程序上查看的页面相关联(例如 www.sport.com)

我的字符串.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">MyApp</string>
    <string-array name="Name_Fav"></string-array>
    //Here I would like to store the names of url favorites
    //<item >Sport</item>
    <string-array name="Url_Fav"></string-array>
    //Here I would like to store full url
    //<item >www.sport.com</item>
</resources>

在我的活动中

public void onClick(View v) {
String[] name_url = //Items retrieved from Name_Fav (sport)
String[] url_fav = //items retrieved from Url_FAv (www.sport.com)
//now here I would like to put and retrieve the data from the resource file
}

编辑:我在这里找到了解决方案: Store a List or Set in SharedPreferences

【问题讨论】:

  • 你到底想要什么?准确地写出你的问题
  • -1 不搜索如何使用 SharedPreferences。
  • 我已经阅读了更多示例,但所有示例都从资源文件中检索单个字符串而不是数组字符串,因此不能很好地解决我的问题

标签: android arrays string url sharedpreferences


【解决方案1】:

你读过here的例子吗?

如果您需要存储比简单的首选项更复杂的内容,您可能应该将这些内容存储到database

【讨论】:

  • 我的问题略有不同,因为我不知道要检索的 url 的数量以及它们的名称,我找不到一个仅检索字符串数组的示例,该字符串具有常量名称
  • 不确定我是否完全理解你的问题,但我已经修改了我的答案以尝试解决它。​​
猜你喜欢
  • 2021-06-29
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 2021-11-15
  • 2023-03-09
  • 1970-01-01
相关资源
最近更新 更多