Reputation: 31
I have about 70 strings that I would like to add to a String Array.. Then print the String array in a TextView..
I have did some homework and I think I actually want to add the strings to a ListArray.
The Strings are located in the string.xml file.
TextView tvHerb = (TextView) findViewById(R.id.tvHerb);
ArrayList<String> herbList = new ArrayList<String>();
herbList.add(R.string.angelica, null);
herbList.add(R.string.anise_seed, null);
tvHerb.setText(herbList.toString());
I get a runtime error as soon as the layout is displayed on emulator.
I am LOST!!! I am also new to android programming so any help at all would be appreciated.
Upvotes: 2
Views: 253
Reputation: 722
First I agree with agreco's answer about the number of attributes.
But also, if you are adding in your arraylist only Strings which come from you string ressource, I think it would be better to define a array of String directly in your strings.xml file.
However, I guess your problem comes from the fact that you apply toString to an ArrayList. toString is a generic method, and I'm not sure of its behavior with an array. toString does not necessarily fits your exact needs, and most of the time it does not.
I think you should better do something like :
String stringToInsert =herbList.get(0);
for (int i=1;i<herbList.size();i++)
{
stringToInsert+="ANY_SEPARATOR_YOU_WANT" + herbList.get(i);
}
tvHerb.setText(stringToInsert);
Upvotes: 0
Reputation: 179
Instead of having each string with id in string.xml file. you can declare a string array in string.xml
`<resources>
<string-array name="sample">
<item>YourValue1</item>
<item>YourValue2</item>
.....
<item>YourValue70</item>
</string-array>
</resources>`
Then in your activity
`String[] sample = getResources().getStringArray(R.array.sample);
ArrayList<String> arrayList = new ArrayList<String>(sample.length);
arrayList = Arrays.asList(sample);`
Upvotes: 0
Reputation: 33544
You need to use context.getString(id)
to get the string resources.
Also is there some reason you're using the 2 argument version of .add
?
You probably want herbList.add( context.getString( R.string.angelica ) );
Upvotes: 1