Access a resource name programmatically

前端 未结 2 1448
你的背包
你的背包 2020-12-06 17:29

I have many string arrays in my resource files, and I want to access them programmatically depending on user input.

int c = Getter.getCurrentNumber();
Strin         


        
2条回答
  •  再見小時候
    2020-12-06 17:48

    You can get the resource id like so

    int c = Getter.getCurrentNumber();
    String resource = "n_" + c;
    int id = getResources().getIdentifier(resource, "array", "com.your.project");
    

    Then just use that id

    String[] info = getResources().getStringArray(id);
    

    Have a look here for another example on getResources().getIdentifier().

提交回复
热议问题