0

I´m sorry to post such an stupid question, but I cannot solve it by myself.

I´m working on a quiz or something like it with radiogroups and radiobuttons. I´ve made 100 questions in strings.xml and each have name like QA1, QA2 and so on...also the radiobuttons like RGAA1,RGAB1,RGAC1 RGAA2,RGAB2,RGAC2, each question have different text and also the ansvers are different. My question is how to make it in the easiest way to change the questions text and also the answers text by each click on next button....

I´ve made it using if where I have an integer p which is incresing by click and if p==1 then .... setText(QA1) if p==2 setText(QA2) ... I want something like:

if p==1 then setText(R.string.QA "and the number of p")

here is an example:

if (p==1) {
        TextView Question = (TextView) findViewById(R.id.Question);
        Question.setText(R.string.QA1);
        RadioButton RA = (RadioButton) findViewById(R.id.RA);
        RA.setText(R.string.RGAA1);
        RadioButton RB = (RadioButton) findViewById(R.id.RB);
        RB.setText(R.string.RGAB1);
        RadioButton RC = (RadioButton) findViewById(R.id.RC);
        RC.setText(R.string.RGAC1);
}

thx

Charles Caldwell
  • 16,649
  • 4
  • 40
  • 47
MacKo
  • 3
  • 1
  • it could be just I´ve never worked with it and after a few looks it seems pretty tough...I´m not so experienced... – MacKo Feb 04 '17 at 21:17

1 Answers1

0

You can get a resource by a string like so

int p = 1;
int resId = getResources().getIdentifier("QA"+p, "string", getPackageName());
Question.setText(resId);

resId will be the value of R.string.QA1

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Man you are a life saver you just save me a bilion lines ....working great really really thanks.... – MacKo Feb 04 '17 at 21:28
  • More solutions here. http://stackoverflow.com/questions/7493287/android-how-do-i-get-string-from-resources-using-its-name – OneCricketeer Feb 04 '17 at 21:32