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 Rebuilding from Scratch All About @string Resources

Blog Reader Help in Understanding

In the creation of the blog reader, in the MainListActivity, why did we need to add the line:

Resources resources = getResources(); mAndroidNames = resources.getStringArray(R.array.android_names);

When the Toast we added was simply:

Toast.makeText(this, getString(R.string.no_items), Toast.LENGTH_LONG).show();

I guess I just don't understand what/why the Resources resources was needed or what it does exactly.

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Excellent question!

In the Toast line, get getString() method is from the Activity class. Since our code is in an Activity file, we can reference it without a dot or anything.

The getStringArray() method, however, is not a method in the Activity class. It's in the Resources class. So we need more information to reference it. Specifically, we need a Resources object (which we name "resources").

What's confusing is that the getResources() method is an Activity method. It returns a Resources object, which we store in the variable named resources.

So really the difference is simply that getStringArray() is in another class, and we need to do some extra work to get an instance of that class and use it in an Activity.