使用 xml 引用字符串数组资源中的字符串

发布于 2024-10-02 09:26:47 字数 246 浏览 0 评论 0原文

我有一些偏好设置,您可以在其中启用/禁用菜单上显示的项目。有 17 项。我在 value/arrays.xml 中创建了一个字符串数组,其中包含这 17 个项目中每一个的标题。

我有preferences.xml,其中包含我的首选项文件的布局,我想引用字符串数组中的单个项目来用作标题。

我该怎么做?

在 Android 开发人员参考中,我了解了如何使用 XML 引用单个字符串,但没有了解如何从 XML 中的数组资源引用字符串。

I have preferences where you can enable/disable what items will show up on the menu. There are 17 items. I made a string array in values/arrays.xml with titles for each of these 17 items.

I have preferences.xml which has the layout for my preferences file, and I would like to reference a single item from the string array to use as the title.

How can I do this?

In the Android developer reference, I see how I can reference a single string with XML, but not how I can reference a string from an array resource in XML.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

等风来 2024-10-09 09:26:48

简而言之:我认为你不能,但似乎有一个解决方法:

如果您在此处查看 Android 资源:

http://developer. android.com/guide/topics/resources/string-resource.html

您会看到,在数组部分(至少是字符串数组)下,“RESOURCE REFERENCE”(从 XML 中获取)未指定解决个别项目的方法。您甚至可以尝试在 XML 中使用“@array/yourarrayhere”。我知道在设计时你会得到第一个项目。但如果你想使用,比如说……当然,第二个,那没有实际用处。

但是,您可以使用一个技巧。请参阅此处:

在 XML 中引用 XML 字符串数组 (Android)

您可以通过在数组定义内寻址独立字符串来“欺骗”(不是真正的)数组定义。例如,在您的 strings.xml 中:

<string name="earth">Earth</string>
<string name="moon">Moon</string>

<string-array name="system">
    <item>@string/earth</item>
    <item>@string/moon</item>
</string-array>

通过使用它,您可以在“android:text”和“android:title”XML 字段中正常使用“@string/earth”和“@string/moon”,但您赢了不要失去将数组定义用于您最初想要的任何目的的能力。

似乎可以在我的 Eclipse 上工作。你为什么不尝试告诉我们它是否有效? :-)

In short: I don't think you can, but there seems to be a workaround:.

If you take a look into the Android Resource here:

http://developer.android.com/guide/topics/resources/string-resource.html

You see than under the array section (string array, at least), the "RESOURCE REFERENCE" (as you get from an XML) does not specify a way to address the individual items. You can even try in your XML to use "@array/yourarrayhere". I know that in design time you will get the first item. But that is of no practical use if you want to use, let's say... the second, of course.

HOWEVER, there is a trick you can do. See here:

Referencing an XML string in an XML Array (Android)

You can "cheat" (not really) the array definition by addressing independent strings INSIDE the definition of the array. For example, in your strings.xml:

<string name="earth">Earth</string>
<string name="moon">Moon</string>

<string-array name="system">
    <item>@string/earth</item>
    <item>@string/moon</item>
</string-array>

By using this, you can use "@string/earth" and "@string/moon" normally in your "android:text" and "android:title" XML fields, and yet you won't lose the ability to use the array definition for whatever purposes you intended in the first place.

Seems to work here on my Eclipse. Why don't you try and tell us if it works? :-)

童话 2024-10-09 09:26:48

也许这会有所帮助:

String[] some_array = getResources().getStringArray(R.array.your_string_array)

因此,您将数组列表作为 String[] 获取,然后选择任何 i,some_array[i]。

Maybe this would help:

String[] some_array = getResources().getStringArray(R.array.your_string_array)

So you get the array-list as a String[] and then choose any i, some_array[i].

惟欲睡 2024-10-09 09:26:48

更好的选择是仅使用资源返回的数组作为数组,
含义:

getResources().getStringArray(R.array.your_array)[position]

这是其他提到的方法的快捷方法,但可以按照您想要的方式工作。否则,Android 不会为基于 XML 的数组提供直接的 XML 索引。

The better option would be to just use the resource returned array as an array,
meaning:

getResources().getStringArray(R.array.your_array)[position]

This is a shortcut approach of other mentioned approaches but does the work in the fashion you want. Otherwise Android doesn't provide direct XML indexing for XML based arrays.

狼亦尘 2024-10-09 09:26:48

不幸的是:

  • 您似乎无法使用 XML 引用 value/arrays.xml 中数组中的单个项目。当然,在 Java 中可以,但在 XML 中不行。 Android 开发人员参考中没有关于这样做的信息,而且我在其他任何地方都找不到任何信息。

  • 您似乎无法在首选项布局中使用数组作为键。每个键都必须是具有自己的键名称的单个值。

我想要实现的目标:
我希望能够循环遍历 17 个首选项,检查该项目是否已选中,如果是,则从字符串数组中加载该首选项名称的字符串。

这是我希望完成此任务的代码:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());  
ArrayAdapter<String> itemsArrayList = new ArrayAdapter<String>(getBaseContext(),   android.R.layout.simple_list_item_1);  
String[] itemNames = getResources().getStringArray(R.array.itemNames_array);  


for (int i = 0; i < 16; i++) {  
    if (prefs.getBoolean("itemKey[i]", true)) {  
        itemsArrayList.add(itemNames[i]);  
    }  
} 

我做了什么:

  • 我为每个项目设置了一个字符串,并在 .我对首选项布局复选框标题使用单个字符串引用,对循环

  • 为了循环浏览首选项,我只是将键命名为 key1、key2、key3 等。由于您使用字符串引用键,因此您可以选择在运行时“构建”键名称。

这是新代码:

for (int i = 0; i < 16; i++) {  
        if (prefs.getBoolean("itemKey" + String.valueOf(i), true)) {  
        itemsArrayList.add(itemNames[i]);  
    }  
}

Unfortunately:

  • It seems you can not reference a single item from an array in values/arrays.xml with XML. Of course you can in Java, but not XML. There's no information on doing so in the Android developer reference, and I could not find any anywhere else.

  • It seems you can't use an array as a key in the preferences layout. Each key has to be a single value with it's own key name.

What I want to accomplish:
I want to be able to loop through the 17 preferences, check if the item is checked, and if it is, load the string from the string array for that preference name.

Here's the code I was hoping would complete this task:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());  
ArrayAdapter<String> itemsArrayList = new ArrayAdapter<String>(getBaseContext(),   android.R.layout.simple_list_item_1);  
String[] itemNames = getResources().getStringArray(R.array.itemNames_array);  


for (int i = 0; i < 16; i++) {  
    if (prefs.getBoolean("itemKey[i]", true)) {  
        itemsArrayList.add(itemNames[i]);  
    }  
} 

What I did:

  • I set a single string for each of the items, and referenced the single strings in the . I use the single string reference for the preferences layout checkbox titles, and the array for my loop.

  • To loop through the preferences, I just named the keys like key1, key2, key3, etc. Since you reference a key with a string, you have the option to "build" the key name at runtime.

Here's the new code:

for (int i = 0; i < 16; i++) {  
        if (prefs.getBoolean("itemKey" + String.valueOf(i), true)) {  
        itemsArrayList.add(itemNames[i]);  
    }  
}
撑一把青伞 2024-10-09 09:26:48

另一种方法是在 strings.xml 中定义资源数组,如下所示。

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE resources [
    <!ENTITY supportDefaultSelection "Choose your issue">
    <!ENTITY issueOption1 "Support">
    <!ENTITY issueOption2 "Feedback">
    <!ENTITY issueOption3 "Help">
    ]>

然后使用上述资源定义一个字符串数组

<string-array name="support_issues_array">
        <item>&supportDefaultSelection;</item>
        <item>&issueOption1;</item>
        <item>&issueOption2;</item>
        <item>&issueOption3;</item>
    </string-array>

您也可以将相同的字符串引用到其他 xml 中,同时保持 DRY 完整。
我看到的优点是,只需更改单个值,就会影响代码中的所有引用。

Another way of doing it is defining a resources array in strings.xml like below.

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE resources [
    <!ENTITY supportDefaultSelection "Choose your issue">
    <!ENTITY issueOption1 "Support">
    <!ENTITY issueOption2 "Feedback">
    <!ENTITY issueOption3 "Help">
    ]>

and then defining a string array using the above resources

<string-array name="support_issues_array">
        <item>&supportDefaultSelection;</item>
        <item>&issueOption1;</item>
        <item>&issueOption2;</item>
        <item>&issueOption3;</item>
    </string-array>

You could refer the same string into other xmls too keeping DRY intact.
The advantage I see is, with a single value change it would effect all the references in the code.

衣神在巴黎 2024-10-09 09:26:48

答案很容易实现。

String[] arrayName = getResources().getStringArray(R.array.your_string_array);

现在您可以通过索引访问数组的任何元素(假设第 i 个索引),然后您可以通过 arrayName[i] 访问它

我希望您理解这一点

The answer is quite easy to implement.

String[] arrayName = getResources().getStringArray(R.array.your_string_array);

and now you can access any element of the array by index (let suppose i'th index), then you can access it by arrayName[i]

I hope you understand this

初心未许 2024-10-09 09:26:48

Kotlin 扩展函数 - 可重用

fun Context.stringArray(array: Int): Array<String> {
    return resources.getStringArray(array)
}

Kotlin Extension Function - Reusable

fun Context.stringArray(array: Int): Array<String> {
    return resources.getStringArray(array)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文