سؤال

I'm new to Android and trying to understand the code I wrote from a tutorial. Meanwhile, I'm referencing the documentation whenever I want to really understand something.

The documentation in question

That page discusses string resources.

For a string, under the String section it states I could retrieve a string defined in an XML file like this: String string = getString(R.string.hello);.

XML in question:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello!</string>
</resources>

Now, for a string array, under the String Array section it states I could retrieve a string string array defined in an XML file like this::

Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

XML in question:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>
</resources>

How come I have to do Resources res = getResources(); for a String Array, and not for a String in an XML file?

Would String[] planets = getStringArray(R.array.planets_arry); not work? Do resources have more methods, namely for more complex cases like String Arrays? is getString so simple it is fine to not need it under a resource? Where could I find a list separating what requires me to do Resources res = getResources(); versus what doesn't require that?

Lastly, does getResources get me every single resource in my Android project irregardless of what XML file it may be in?

هل كانت مفيدة؟

المحلول

How come I have to do Resources res = getResources(); for a String Array, and not for a String in an XML file?

getString() is a method of your Context object (mostly an Activity but could also be a Service). The code for that method is:

public final String getString(int resId) {
    return getResources().getString(resId);
}

Meaning getString() is just a convenience method that the Context class offers. In the end both calls do the same, namely retrieve a resource through the Resource object.

Would String[] planets = getStringArray(R.array.planets_array); not work?

No because Context doesn't have a convenience method to retrieve String[].

Do resources have more methods, namely for more complex cases like String Arrays?

Yes. See http://developer.android.com/reference/android/content/res/Resources.html. I leave if up to you to decide whether they are more complex than getStringArray.

is getString so simple it is fine to not need it under a resource?

Resource does have a getString(int) method: http://developer.android.com/reference/android/content/res/Resources.html#getString(int)

Where could I find a list separating what requires me to do Resources res = getResources(); versus what doesn't require that?

There's no such list. The JavaDoc for Context will tell you what convenience method you got: http://developer.android.com/reference/android/content/Context.html

Lastly, does getResources get me every single resource in my Android project regardless of what XML file it may be in?

Only if it's in a resource xml file. Layouts or Drawables can't contain String resources.

نصائح أخرى

The call getString() is essentially the same thing as getResources().getStringArray(), as mentioned here the method getString() is just a convenience method, because it's used so often.

public final String getString(int resId) {
    return getResources().getString(resId);
}

I'll also add that you can't use getStringArray in other resource files, like XML layouts, where getString() can be.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top