将 Strings.xml 中的 String-Array 更改为 ArrayList [英] Change String-Array in Strings.xml to ArrayList

查看:20
本文介绍了将 Strings.xml 中的 String-Array 更改为 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Android 应用.我需要将字符串数组转换为 ArrayList.我已经阅读了这一点,并且都有将值添加到 java 文件中的数组的情况.我已经在strings.xml 文件中声明了字符串数组.我的字符串数组在这里:

I'm developing an Android app. I need to convert a string array into an ArrayList. I've read up on this, and all have cases where you add the values to the array in the java file. I have declared the string-array in the strings.xml file. My string-array is here:

<string-array name="Lines">
    <item>Red Line</item>
    <item>Blue Line</item>
    <item>Orange Line</item>
    <item>Green Line</item>
    <item>Brown Line</item>
    <item>Purple Line</item>
    <item>Pink Line</item>
    <item>Yellow Line</item>
</string-array>

我需要将 Lines 转换为 ArrayList.如果我用这个

I need to convert Lines into ArrayList. If I use this

List<String> Lines = new ArrayList<String>();

要声明ArrayList,如何通过ID找到数组列表,并将其存储为Lines?

to declare the ArrayList, how can find the array list by ID, and store that as Lines?

推荐答案

试试这个;

List<String> Lines = Arrays.asList(getResources().getStringArray(R.array.Lines));

这对于 kotlin:

and this for kotlin:

val Lines = resources.getStringArray(R.array.Lines).toList()

这篇关于将 Strings.xml 中的 String-Array 更改为 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆