Java 从XML数组读取SharedReferences默认值

Java 从XML数组读取SharedReferences默认值,java,android,eclipse,sharedpreferences,Java,Android,Eclipse,Sharedpreferences,我试图让我的应用程序从XML数组中读取SharedReferences默认值,但我在实现这一点时遇到了一个问题。比如说,我有20个复选框,我在strings.xml中的字符串数组中插入了20个项目。现在我想做的很简单,我想让我的SharedReferences从这个数组中读取默认值。Checkbox1将获得第一个项目名称,checkbox2将获得第二个项目名称,依此类推。下面的代码显示了我尝试执行的操作 XML数组: <string-array name="spBifrost">

我试图让我的应用程序从XML数组中读取SharedReferences默认值,但我在实现这一点时遇到了一个问题。比如说,我有20个复选框,我在strings.xml中的字符串数组中插入了20个项目。现在我想做的很简单,我想让我的SharedReferences从这个数组中读取默认值。Checkbox1将获得第一个项目名称,checkbox2将获得第二个项目名称,依此类推。下面的代码显示了我尝试执行的操作

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>
现在,当我实际使用这段代码时,它根本无法按我所希望的方式工作。例如,它没有将checkbox1重命名为“精心制作的图腾(250)”,而是将其重命名为一组我不理解的随机数。有人能告诉我我做错了什么吗?我是一个完全的初学者(一个月前开始学习java/android开发),所以我很有可能完全错了,这就是为什么我请求您的帮助

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());
}
私有字符串getItemQuantity(字符串键){
SharedReferences itemQuantitySP=getApplicationContext().GetSharedReferences(“bifrostPrefs”,android.content.Context.MODE\u PRIVATE);
Resources res=getResources();
String[]spBifrost=res.getStringArray(R.array.spBifrost);
ArrayList spBifrostArray=新的ArrayList();
返回itemQuantitySP.getString(key,spBifrostArray.toString());
}

询问前请先搜索文档

如中所示,应该使用

Resources res = getResources();
String[] spBifrost = res.getStringArray(R.array.spBifrost);
当然,为了让自己更轻松,请将其设置为ArrayList:

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

非常感谢您的回答,我现在是这样做的(在我的原始帖子中选择编辑),但它仍然无法正常工作。默认值现在显示为[]。你知道怎么回事吗?你解决问题了吗?遗憾的是,还没有,我觉得我离解决方案更近了,但我的应用程序仍然无法读取阵列。正如我已经告诉LJoosse(回答我问题的人)的那样,默认值现在显示为“[]”。
Resources res = getResources();
String[] spBifrost = res.getStringArray(R.array.spBifrost);
ArrayList spBifrost = new ArrayList<String>(spBifrost);