0

I want to design a ListView that will be populated from a StringArray resource depending on which button was clicked. The array name will be retrieved and put into the getStringArray method like 'dynamically', after the button was clicked.

I'm trying this method to get the identifier by name:

getResources().getIdentifier(String name, String defType, String defPackage);

but when I am constructing the array name as a String made of clicked button ID and some other Strings like this:

Bundle extras = getIntent().getExtras();
Integer buttonId = extras.getInt("ButtonId");
String resourceName = "";
int resourceId = 0;

resourceName = "R" + buttonId.toString() + "WEEK" + "OUT";
resourceId = getResources().getIdentifier(resourceName, "array", this.getPackageName());

I get an error that there is no such resource and the getIdentifier method returns 0. (The name of the resource is set to the same value as the resourceName).

Dan Teesdale
  • 1,823
  • 15
  • 26
teemgie
  • 11
  • 5
  • 1
    if the resource does not exist it will return 0. check this http://stackoverflow.com/questions/17470360/parse-r-java-class/17470384#17470384 – Raghunandan Jul 09 '13 at 03:38
  • Seems like you're complicating your life. Why don't you just create a final static Map of Button IDs to String Array IDs? That way you get the benefit of knowing that everything exists at compile time. – dmon Jul 09 '13 at 04:36
  • Thanks. You really helped me. Now it's working fine. Although I used SparseIntArray instead of a Map. I guess you're right and I was complicating my life a little bit. – teemgie Jul 09 '13 at 15:14

0 Answers0