简体   繁体   中英

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. Let's say for example that I have 20 checkboxes, I inserted 20 items to string-array in strings.xml. Now what I'm trying to do is simple, I want my sharedpreferences to read default values from this array. Checkbox1 will get the first item name, checkbox2 will get the second item name and so on. The code below shows what I tried to do.

XML array:

<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:

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. 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 code now:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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