Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

stefano cerelli
Courses Plus Student 612 Points[FunFacts Android App] problem on getting data.
Hi everybody. I just ended the first android course and i realize my super cool fun facts app. Before moving to next course i decided to a dive a little bit deeper in android coding by evolving this app.
Since now i had no problem with coding but now i'm little bit stucked; What i'm trying to achieve is grabbing Facts from an xml instead of an array. And that's fine because i did it but now i would like change things once more.
This is my starting point: i realized this xml and coded a function that grab the file, parse xml into an array and then it return the fact:
<?xml version="1.0" encoding="utf-8"?>
<resources name>
<string-array name="facts">
<item>facts1</item>
<item>fact2</item>
<item>fact3</item>
</string-array>
</resources>
public class FactBook{
public String getFact(String[] facts){
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(facts.length);
return facts[randomNumber];
}
}
Resources res = getResources();
final String[] xmlToArray = res.getStringArray(R.array.facts);
String fact = mFactBook.getFact(xmlToArray);
factLabel.setText(fact);
and that's fine. Now i want to associate an image to each block of facts that will be displayed just above the fact as category image and this is my xml now:
<?xml version="1.0" encoding="utf-8"?>
<resources name="facts">
<string-array name="category1">
<item>facts1</item>
<item>fact2</item>
<item>fact3</item>
</string-array>
<string-array name="category2">
<item>fact1</item>
<item>fact2</item>
<item>fact3</item>
<item>fact4</item>
</string-array>
</resources>
This one (OFC) not working anymore and i can't even find the proper way to do it.
final String[] xmlToArray = res.getStringArray(R.array.facts);
Any tips or suggestion on how to do it? and what you think about my previous code, is it bad? Thanks a lot.
2 Answers

Gloria Dwomoh
13,104 PointsInitially you did ...
<resources name>
<string-array name="facts">
and then suddenly you changed it to ...
<resources name="facts">
<string-array name="category1">
then you called
final String[] xmlToArray = res.getStringArray(R.array.facts);
there, facts is no more the name of the string array, which makes that line stop working.
Also you shouldn't put an attribute to the Resource element because it is the root node. Read more on that here String Resources.

Gloria Dwomoh
13,104 PointsI suggest you do a multidimensional resource array. Specifically a 2D resource array. I think this link might be helpful to you on that Multidimensional resource arrays in Android. You can also check this How to retrieve 2D array from xml string resource for Android?.
stefano cerelli
Courses Plus Student 612 Pointsstefano cerelli
Courses Plus Student 612 PointsHi! If your read the note i left up there i said that i would like to categorize the strings so that's why i changed the xml into this.
so what you suggest me to do? at end i would like to have an array like this. <resources name="facts"> <string-array name="category1">
[category1][0] = "" [category1][1] = "" [category1][2] = ""
[category2][0] = "" [category2][1] = "" [category2][2] = ""