簡體   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