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 trial

Android Build a Blog Reader Android App Adapting Data for Display in a List Cleaning Up Our Presentation

Is getString() method is necessary for getting a string resource ?

I tried

Syntax: emptyTextView.setText(R.string.no_items);

instead of

Syntax: emptyTextView.setText(getString(R.string.no_items));

code in BlogReader app. But it is working fine. I just want to know , whats the significant of using getString() method for getting a string from string resource?. Is there any possibility for getting errors in future if I'm not using this method?

Refael Ozeri
Refael Ozeri
18,274 Points

According to the Android Developer documentation, you should use the getString() method in order to extract a string from the XML. Truth be told I have no idea what you mean by getting any "errors in the future". as one coder to another, I recommend sticking to the documentation and guidelines.

here's a link to the Strings documentation if you would like to look further into it. http://developer.android.com/guide/topics/resources/string-resource.html

Good luck! and happy coding :D

Thanks alot Erez for your kind reply and advice.So there is no specific reason for using getString() for extracting a string from XML other than it is specified in android documents, right? I'm very curious about each and every line of code. I'm entirely new to android and XML. It makes me feel I will make mistakes.Not confident yet!!! But sure, I will follow the guidelines for coding.

Happy coding for you too... :)

1 Answer

Yes, within the Activity context, you must use getString(int) or getText(int) to get the string resource.

It only looks like you don't need to do that for TextView.setText() because that class is hiding the call. If you look into the TextView source code, then you can see that there are multiple "overloaded" implementations of setText(). For example, here's a snippet from the TextView source code:

public final void setText(CharSequence text) {
    // Code to set the text here.
}

public final void setText(int resid) {
    setText(getContext().getResources().getText(resid));
}

So, depending on what datatype you pass as the argument to TextView.setText(), a different method will be called. Some classes in Android allow either type of input, some only allow the String. But, either way, the same thing will eventually be called.

I hope this clears it up for you more.

More info: http://developer.android.com/guide/topics/resources/string-resource.html

ps - In the Activity context, getText(int) is just like getString(int), except that it returns a CharSequence instead of a String. So, that allows you to have some extra styling in the string resources file.

Thank you very much Goodwin.

Now I'm clear about setText() and getString().