1

I have an abstract class which contains method for setting header text. It looks like:

TextView header = (TextView) findViewById(R.id.hTitle);
header.setText(getString(this.getHeaderStringID()));

At this moment methods that override the abstract getHeaderStringID() return integers like 0x7f040001. 1 I wonder whether there's a way to utilize concatenation or somewhat similar to achieve this:

header.setText(getString(R.string.!getHeaderStringID!));

In desired case getHeaderStringID would return string like "sAboutHeader"

2 I'm new to Java - can I get rid of creating header object. If I don't do like that, eclipse validator says that the appropriate method isn't found and doesn't let me launch app.

1 Answer 1

1

If I understand you correctly, I think that you should use reflection:

try {
        Field idField = R.String.class.getDeclaredField(getHeaderStringID());
        int value = idField.getInt(idField);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    } 

the value variable could be given as an input to the getString() function.

Sign up to request clarification or add additional context in comments.

2 Comments

it works like that now, but it returns integer. What I want to do is to set to each subclass it's own string identifier in order to achieve header.setText(getString(R.string. here comes some magic that appends my string identifier to R.string. ));
After you'd manage to apend the unique string to R.string, it would return you an integer as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.