0

I have a number being generated from a date. I have some string with content I need to pull.

string name would be something like, week_8. 8 being the generated number. Im not quite sure how I could go about adding it to my java. See below, the week_+diff. Thank you

String desc = getResources().getString(R.string.week_+diff);
        d = (TextView) view.findViewById(R.id.description);
        d.setText(desc);
ValhallaSkies
  • 61
  • 1
  • 12
  • Not sure what you are trying to display to the user. If you don't have a string name week_8 in resource, it would not have the string content. If you do have a string resource named week_8 though, then you would use a manual mapping to the string resource name. Like Map numToResIdMap = new HashMap<>(); And add in your manual mappings. – uDevel Jan 29 '19 at 23:33
  • It's a bit hard to tell from your question, but perhaps you want `String desc = getResources().getString("week_" + diff);` – dave Jan 29 '19 at 23:33
  • To do specifically what you're asking, have a look at [this answer](https://stackoverflow.com/a/3476470). In the examples there, you would replace the second argument with `"string"`, and the `int` returned from `getIdentifier()` is what you would use in place of the `R.string` in `getString()`. The second linked duplicate has a complete solution in [this answer there](https://stackoverflow.com/a/11595723). – Mike M. Jan 29 '19 at 23:39

1 Answers1

1

Follow these simple steps.

1: create array of String in array.xml

<?xml version="1.0" encoding="utf-8"?>  
    <resources>  
        <string-array name="testArray">  
            <item>first</item>  
            <item>second</item>  
            <item>third</item>  
            <item>fourth</item>  
            <item>fifth</item>  
        </string-array>
    </resources>

2: Getting this array from resources

 String[] test;
test = getResources().getStringArray(R.array.testArray);

3: Now finally get value of your choice

 d = (TextView) view.findViewById(R.id.description);
        d.setText(test[diff]);
umer farooq
  • 183
  • 2
  • 7