简体   繁体   English

从XML数组中读取sharedpreferences默认值

[英]Read sharedpreferences default values from XML array

I'm trying to make my app read sharedpreferences default values from XML array but I have a problem achieving that. 我试图使我的应用程序从XML数组中读取sharedpreferences默认值,但在实现该目标时遇到问题。 Let's say for example that I have 20 checkboxes, I inserted 20 items to string-array in strings.xml. 例如,假设我有20个复选框,我在string.xml中的string-array中插入了20个项目。 Now what I'm trying to do is simple, I want my sharedpreferences to read default values from this array. 现在我想做的很简单,我希望我的sharedpreferences从这个数组中读取默认值。 Checkbox1 will get the first item name, checkbox2 will get the second item name and so on. Checkbox1将获得第一个项目名称,checkbox2将获得第二个项目名称,依此类推。 The code below shows what I tried to do. 下面的代码显示了我尝试执行的操作。

XML array: XML阵列:

<string-array name="spBifrost">
    <item>Elaborate Totem (250)</item>
    <item>Pile of Crystalline Dust (250)</item>
    <item>Powerful Venom Sac (250)</item>
    <item>Vial of Powerful Blood (250)</item>
    <item>Ancient Bone (250)</item>
    <item>Armored Scale (250)</item>
    <item>Vicious Claw (250)</item>
    <item>Vicious Fang (250)</item>
    <item>Glob of Ectoplasm (77)</item>
    <item>Glob of Ectoplasm (77)</item>
    <item>Mystic Coin (77)</item>
    <item>Obsidian Shard (77)</item>
    <item>Philosophers Stone (462)</item>
    <item>Badge of Honor (500)</item>
    <item>Obsidian Shard (250)</item>
    <item>Shard of Zhaitan (500)</item>
    <item>Opal Orb (100)</item>
    <item>Pile of Crystalline Dust (250)</item>
    <item>Unidentified Dye (250)</item>
    <item>Pile of Crystalline Dust (250)</item>
    <item>Pile of Incandescent Dust (250)</item>
    <item>Pile of Luminous Dust (250)</item>
    <item>Pile of Radiant Dust (250)</item>
    <item>Icy Runestone (100)</item>
</string-array>

Sharedpreferences get code in java: Sharedpreferences在Java中获取代码:

private String getItemQuantity(String key){
    SharedPreferences itemQuantitySP = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE);
    Resources spRes = getResources();
    TypedArray itemNames = spRes.obtainTypedArray(R.array.spBifrost);
    String itemSp = itemNames.toString();
    return itemQuantitySP.getString(key, itemSp);
}

Now when I actually use this code, it doesn't work how I want it to at all. 现在,当我实际使用此代码时,它根本无法工作。 For example, instead of renaming checkbox1 to "Elaborate Totem (250)" it renames it to a bunch of random numbers that I do not understand. 例如,不是将checkbox1重命名为“ Elternate Totem(250)”,而是将其重命名为一堆我不理解的随机数。 Could someone tell me what I am doing wrong? 有人可以告诉我我在做什么错吗? I am a complete beginner (started learning java/android developing a month ago) so there's a big chance I approached this completely wrong and that's why I'm asking for your help. 我是一个完整的初学者(一个月前开始学习Java / android开发),所以我很有可能完全解决了这个错误,这就是为什么我寻求您的帮助。

Java code now: 现在的Java代码:

private String getItemQuantity(String key){
    SharedPreferences itemQuantitySP = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE);
    Resources res = getResources();
    String[] spBifrost = res.getStringArray(R.array.spBifrost);
    ArrayList<String> spBifrostArray = new ArrayList<String>();
    return itemQuantitySP.getString(key, spBifrostArray.toString());
}

Please search the documentation before asking! 在询问之前,请先搜索文档!

As you can see in here , you should retrieve the string array with 如您在此处看到的那样,您应该使用

Resources res = getResources();
String[] spBifrost = res.getStringArray(R.array.spBifrost);

Of course, to make it a bit easier for yourself, make it an ArrayList: 当然,要使其变得更容易一点,请将其设置为ArrayList:

Resources res = getResources();
String[] spBifrost = res.getStringArray(R.array.spBifrost);
ArrayList spBifrost = new ArrayList<String>(spBifrost);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM