I'm trying to understand an Android app that performs computations on investment portfolios. The portfolios are stored in res/values/portfolio.xml
:
When a button is pressed in the app, the portfolio data is retrieved as follows:
String portfolioName = ((TextView) findViewById(R.id.portfolioName)).getText().toString();
Resources res = getResources();
String[] data = res.getStringArray(res.getIdentifier(portfolioName, "array", this.getPackageName()));
I found the Android documentation on the String Array resource type that explains the syntax of the portfolio.xml
file, and it explains why the name
attribute should be used as the first argument of getIdentifier()
:
“The filename is arbitrary. The
<string-array>
element'sname
will be used as the resource ID.”
But I haven't found any documentation that explains how you know what you're supposed to put for the defType
argument of getIdentifier
(other than that it's a string). In the provided example, "array"
works, but where does it come from? And what are the possible values of 'defType' in general?
getIdentifier
returns the id
of the resource for the given resource name. typeDef refers to the type of the Resource
(read more here). Keep in mind that the content of res
is parsed at compile time and the R.java class is generated from the result of this parsing. In the end what you are looking for is a field declared in that class. I don't know the internal implementation, but if you provide array
as res type, android will look up only on R.array
, instead than on the whole R
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With