Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources.getIdentifier(), possible values of deftype argument?

Tags:

android

I'm trying to understand an Android app that performs computations on investment portfolios. The portfolios are stored in res/values/portfolio.xml:

enter image description here

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?

like image 619
yroc Avatar asked Mar 02 '16 19:03

yroc


1 Answers

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

like image 72
Blackbelt Avatar answered Nov 03 '22 22:11

Blackbelt