繁体   English   中英

如何从strings.xml获取值到arraylist

[英]How to get value from strings.xml to arraylist

我有以下代码:

package com.sample.app;

import android.content.res.Resources;

import java.util.ArrayList;




public class Content {

    public static ArrayList<String> Names;
    public static ArrayList<String> NamesSub;
    public static ArrayList<String> Images;
    public static ArrayList<String> Wallpaper;
    public static ArrayList<String> Audio;
    public static ArrayList<String> BIG_Images;
    public static ArrayList<String> Sub_heading;
    public static ArrayList<String> Description;


    Content(){
        this.Names = new ArrayList<String>();
        this.NamesSub = new ArrayList<String>();
        this.Sub_heading = new ArrayList<String>();
        this.Images = new ArrayList<String>();
        this.Wallpaper = new ArrayList<String>();
        this.BIG_Images = new ArrayList<String>();
        this.Audio = new ArrayList<String>();
        this.Description = new ArrayList<String>();

        this.Description.add(0, "Long description");
        this.Description.add(1, "");
        this.Description.add(2, "");

    }

}

并且对ArrayList描述太长。 当我粘贴它给我的描述时

错误:常量字符串太长

错误。 之后,我尝试从strings.xml设置值。 我正在使用此代码获取值:

this.Description.add(0,String.valueOf(R.string.descvalue));

但是当我运行该应用程序时,它会打印这样的数字2131689519而不是我的值。

之后我尝试了string-array

<string-array name="descvalue">
    <item>too long value</item>
</string-array>

在 java 文件中,我尝试使用以下代码获取值:

this.Description.add(0, getResources().getStringArray(R.array.descvalue)[0]);

但是当我运行该应用程序时,它崩溃了。

那么在这种情况下我应该怎么做呢? 谢谢。

“错误:常量字符串太长”

为此,您可以使用 Text 类

Text text = new Text("foo");
String s = text.getText(); 
text.setText(s); 

代替

this.Description.add(0,String.valueOf(R.string.descvalue));

this.Description.add(0,getString(R.string.descvalue));

但是getString()需要一个context

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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