0

This is a part code of my project in Android Studio:

    String s;

    for(int i=0; i<=3; i++){
        buttons[i].setOnClickListener(new View.OnClickListener() {
            @Override
           public void onClick(View v) {
                 s = getResources().getString(R.string.text1);
                 buttons[i].setText(s);
            }
        });

But I need to do something like:

     s = getResources().getString(R.string.("text" + i));

Is it possible to do something like this?

Narendra
  • 1,511
  • 1
  • 10
  • 20
Ana-Maria
  • 41
  • 7
  • Use the `Resources#getIdentifier()` method to get an ID you can use with `getString()`. https://stackoverflow.com/a/3476470 – Mike M. Mar 24 '18 at 09:08
  • Going by the snippets in your question, it would be something like this: `int resId = getResources().getIdentifier("text" + i, "string", getPackageName());`, `s = getResources().getString(resId);`. You could certainly make that a one-liner, but you really should make sure that `getIdentifier()` does not return `0` before you call `getString()` with it. – Mike M. Mar 25 '18 at 10:22
  • 1
    It's working, thank you! – Ana-Maria Mar 26 '18 at 14:35

0 Answers0