1

More Code - The String Created here is R.string.c#### - c.#### is a pre-defined string i want to run as a setText.

int Q1 = question1.getmCounter();
int Q2 = question2.getmCounter();
int Q3 = question3.getmCounter();
int Q4 = question4.getmCounter();

int qTotal = Q1 + Q2 + Q3 + Q4;
String Test5 = "R.string.c" + qTotal;

This is how i am now getting the "string" i want to feed.

    textOut = (TextView) findViewById(R.id.ChmpNametxt);
    textOut.setText(Test5);

This is where i want to feed it.

1 Answer 1

2

You can access to resources by using the "getIdentifier" method. It allows to access any resource of the package:

getResources().getIdentifier("c1123", "string", this.class.getPackageName());

You can pass any variable in the first argument, this corresponds to the name of your string. The second argument specifies in which resources to search it (string, drawable, layout.....). The third argument is the application's package name. It specifies which "R" is used. In your case, you want to look inside the resources ("R") contained in your application.

Edit: This method returns an int, that correspond to the ID of the resource you're looking for, for exemple R.string.c1123

Edit2: This int should then be used as the parameter for the setText function.

So the complete code would be:

int resId = getResources().getIdentifier("c" + qTotal, "string", this.class.getPackageName());
textOut.setText(resId);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.