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! While you're at it, check out some resources Treehouse students have shared here.
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 trialStephen Ford
2,836 PointsStoring the story and choice text in string.xml
So, I decided that it would make more sense to store my Story text in the string.xml folder instead of inside the Java code. I copy/pasted all the text over into the string folder but left the array in Java. I tried to implement "getText(R.string.page0).toString);" but the "getText" is not being recognized within the constructor "public Story" in the Story.java class where currently the text is currently stored. The bubble message that appears when I place my cursor on "getText" says "Can not resolve method getText(int)".
Is there a fix for this? Do I need to move the entire array into the string.xml folder? Can I leave the array in Java and import the text?
Thanks in advance!
Cheers Stephen
3 Answers
James Simshaw
28,738 PointsHello,
Now that I have taken a look at your code, I would suggest if you want to define the story in strings.xml(very useful if you want to add translations), you should treat it the same way as you do the image resource. This is because getString needs a context to be callable. So in your story, you store the resource id as an int(for example, R.string.page0). You will also need to modify your getter for the string to be a getter for the resource id. Then in your StoryActivity, where you call
String pageText = mCurrentPage.getText();
you would instead want to do something like
String pageText = getString(mCurrentPage.getText());
remembering to change the getter name if you changed it earlier. Let me know if you need more assistance and I will gladly help.
James Simshaw
28,738 PointsHello,
With the line getText(R.string.page0).toString); you actually have two problems. First, the method you want to be calling is getString instead of getText. Second, you don't need to call toString on that result since you will already be getting a String back. This will leave you with something like
getString(R.string.page0);
If you are having further problems, could you post your java files and your string.xml file?
Stephen Ford
2,836 PointsHey James thanks for your suggestion. Sadly the getString method returns the same error. There are only two .get options it seems to like .getPage and .getClass.
I just joined Github
hope that this works :)
Stephen Ford
2,836 PointsNice thanks for the helpful pointers! :)
James Simshaw
28,738 PointsYou're welcome. Have fun with Android Development. Feel free to ask more questions here on the Forums if you run into any further problems.
Stephen Ford
2,836 PointsStephen Ford
2,836 PointsWow, thanks James... being new to the programming world, I will need to ponder over your suggestions before I can fully (or partially) grasp what it is that needs to change.
I am not sure I have the understanding of "getString needs a context to be callable.".
You mention approaching it as with the images. Are you speaking of the images that change per page? I also do have an image issue to resolve so that is I am verifying which set of images you are speaking.
I have 2 different Title Images one in English and one in French but I am not sure how to set it up like Android does with the values for different languages. DO I need to create a second directory of mipmaps for French? It would seem redundant as only 1 image changes to put all of the images in two place.
I truly appreciate your insight. It can truly be hard to wrap your head around when you are just starting to code.
I'm glad that Github worked!
Cheers!
James Simshaw
28,738 PointsJames Simshaw
28,738 PointsI'm essentially referring to handling things as the images that change per page, yes. Its recommended to put icons in the mipmap folders, but other drawables to continue being in the drawable folders(for example, drawable-mdpi, drawable-hdpi, etc). You should be able to make a drawable folder that has a langauge conditon on it in addition to screen size(though that would mean twice as many images for that one page). You could (I haven't tried this), but it should be able to be done with a folder like drawable-fr-xhdpi, and one for each of the different screen densities. What happens is if you are set to French, Android will look in the french drawable set of files for an image(depending upon screen density if you have them broken up that way), then if it doesn't find it there, it will start using more generalized methods by looking in the regular drawable-xhdpi folder, and if it can't find something there, then the drawable folder. Also, if you move your images to drawable, make sure to reference them by R.drawable.yourIdHere.
James Simshaw
28,738 PointsJames Simshaw
28,738 PointsThe short version of my previous comment is, you only need to duplicate the image that is changing, not all of the images.