Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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's name 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 defTypeargument 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

Yes, this document is what I was looking for -- thank you. I will need additional time to assimilate the additional information you have provided. Thanks! – yroc Mar 2, 2016 at 20:19

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.