简体   繁体   中英

Java/Xml/Android - Calling random Strings from .XML String file in the Java

I'm currently developing android quiz app and I implemented the "reset" button that resets the questions and randomize them, but I'm having trouble with calling the random Strings from .xml file in MainActivity.java.

I have all of my questions listed in the Strings.xml file like this, in order to make them easy to translate:

<string name="q1">The reactor at the site of the Chernobyl nuclear disaster is now in which country?</string>
<string name="a1">A. Slovakia</string>
<string name="b1">B. Ukraine</string>
<string name="c1">C. Hungary</string>
<string name="d1">D. Russia</string>
<string name="q1answer">B</string>

All of the questions are listed in that file like q1, q2, q3, and the same goes for ABCD answers: a1, b1, c1, d1; a2, b2, c2, d2 , and so on.

There are many questions, but button chooses only 5 of them and displays them on the screen. The problem is that I just can't access the Strings if I want to use an integer generated by randomizer to find them in .xml :

    for (int i = 1; i <= 5; i++) {

        // I managed to get the identifier of the TextView with the for loop like that (TextViews for questios are listed q1q, q2q, q3q, q4q, q5q):
        // I would like to do the same thing down there with Strings.
        TextView question = (TextView) findViewById(getResources().getIdentifier("q" + i + "q", "id", this.getPackageName()));

        // randomQuestion is the number of the question in random number generator from different method.
        randomQuestion = randomizeNumbers();

        // And here I'm stuck, this will show "q1-randomNumber" instead of the real question, 
        // because it does not see it as an ID. I tried various different solutions, but nothing works.
        question.setText("q" + randomQuestion);

        // I left the most silly approach to show what I mean.
    }

What can I do to make the computer distinguish the name of the String? So it displays the real question instead of "q1" "q6" "q17"?

Thank you for help in advance!

You can access strings from the string.xml file by using the getIdentifier() utility.

  private String getStringByName(String name) {
  int resId = getResources().getIdentifier(name, "string", getPackageName());
  return getString(resId);
}

It would be a lot easier if you also tried another approach entirely and create a Problem class which would look something like :

public class Problem(){
int ID;
String question;
String answer1; String answer2; //..and so on
String answerCorrect;

public class Problem(int ID, String question, ...){
this.question= question;
... 
}
}

And than, create and ArrayList = new ArrayList<>(); and populate it with your questions. Than, you would have the ability to access them by their ID / position in array, search them by name and so on. In the future, you can use this model to request the list of problems from a server too.

question.setText("q" + randomQuestion);

In here, your parameter is inferred as a string since you pass "q" , so it will display "q-randomNumber" as expected. I think what you want is to pass and actual ID to setText . To do this, you can do the same approach you did to find the question textview ID using "string" instead of "id".

In the MainActivity or in a function :

Resources res = getResources();

        myString = res.getStringArray(R.array.YOURXMLFILE);

        String q = myString[rgenerator.nextInt(myString.length)];
// WE GET THE TEXTVIEW
        tv = (TextView) findViewById(R.id.textView15);
        tv.setText(q);
// WE SET THE TEXTVIEW WITH A RANDOM QUESTION

And declare :

private String[] myString;
private static final Random rgenerator = new Random();
private TextView tv;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM