得到一个整数数组从Android程序的XML资源整数、数组、程序、资源

2023-09-06 02:08:34 作者:从未停步

只是一个匆匆,

我在RES /价值/ integers.xml XML资源

i have an xml resource in res/values/integers.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <integer-array name="UserBases">
          <item>2</item>
          <item>8</item>
          <item>10</item>
          <item>16</item>
     </integer-array>
</resources>

和香港专业教育学院尝试了几件事情进行访问:

and ive tried several things to access it:

int[] bases = R.array.UserBases;

这只是返回和INT参考UserBases不是该数组本身

this just returns and int reference to UserBases not the array itself

int[] bases = Resources.getSystem().getIntArray(R.array.UserBases);

这将引发异常回头看我告诉我的INT参考R.array.UserBases点什么

and this throws an exception back at me telling me the int reference R.array.UserBases points to nothing

什么是访问该数组,将其推入一个很好的基础类型为int [],然后可能的任何修改推回XML资源的最佳方式。

what is the best way to access this array, push it into a nice base-type int[] and then possibly push any modifications back into the xml resource.

我检查了Android的文件,但我没有发现任何东西非常富有成果的。

I've checked the android documentation but I haven't found anything terribly fruitful.

推荐答案

您需要使用资源来获得int数组;但是,你正在使用的系统资源,只包括标准Android资源(例如,那些通过 android.R.array访问。* )。要想让自己的资源,你需要通过你的上下文中的一个来访问的资源。

You need to use Resources to get the int array; however you're using the system resources, which only includes the standard Android resources (e.g., those accessible via android.R.array.*). To get your own resources, you need to access the Resources via one of your Contexts.

例如,所有的活动都是上下文,所以在活动中,你可以这样做:

For example, all Activities are Contexts, so in an Activity you can do this:

Resources r = getResources();
int[] bases = r.getIntArray(R.array.UserBases);

这就是为什么它是非常有用的,以绕过语境;你需要它来获取一个保持应用程序的资源。

This is why it's often useful to pass around Context; you'll need it to get a hold of your application's Resources.