简体   繁体   中英

How to get value from strings.xml to arraylist

I have the following code:

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, "");

    }

}

And have a too long description for ArrayList . when I paste the description it gave me

error: constant string too long

error. After that i tried setting values from strings.xml . I am using this code for getting values:

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

But when I run the app it prints numbers like this 2131689519 not my values.

After that i tried string-array

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

And in the java file, I tried getting values with this code:

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

But when I run the app it's crashing.

So what should I do in this case? Thanks.

"error: constant string too long"

for this, you can go with Text class

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

Replace

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

With

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

but getString() needs a context .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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