0

Hello I have 2000 Questions each with 3 possible answers in my Strings.xml. I want that a random question is displayed in the textview with is answers.

<string name="Frage1">Was versteht man unter defensivem Fahren?</string>
<string name="ersteAntwort1">Nicht auf dem eigenen Recht bestehen</string>
<string name="zweiteAntwort1">Mit Fehlern anderer rechnen</string>
<string name="dritteAntwort1">Vorsorglich an jeder Kreuzung anhalten</string>
<string name="Frage2">Was kann zu Auffahrunfällen führen?</string>
<string name="ersteAntwort2">Unerwartet starkes Bremsen</string>
<string name="zweiteAntwort2">Unaufmerksamkeit</string>
<string name="dritteAntwort2">Zu dichtes Auffahren</string>
<string name="Frage3">Sie fahren innerorts hinter einem Fahrzeug mit ortsfremdem Kennzeichen. Was könnte geschehen?</string>
<string name="ersteAntwort3">- bremst unerwartet</string>
<string name="zweiteAntwort3">- betätigt den Blinker vor dem Abbiegen zu spät</string>
<string name="dritteAntwort3">- hält unerwartet an, um nach dem Weg zu fragen</string>  

The names of the Strings are always the same only the number of the question changes so i want to add a random number to the first part of the string name

public void neueFrage (View view) {

    Button buttonTipp = (Button)findViewById(R.id.button1);
    final TextView tw = (TextView)findViewById(R.id.textView1);
    final CheckBox Chk1 = (CheckBox)findViewById(R.id.checkBox1);
    final CheckBox Chk2 = (CheckBox)findViewById(R.id.checkBox2);
    final CheckBox Chk3 = (CheckBox)findViewById(R.id.checkBox3);
    buttonTipp.setOnClickListener(new OnClickListener() {   
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int random = (int) (Math.random() *3 );
            String zahl = String.valueOf(random);
            String question = "Frage"+zahl;
            String firstAnswer ="ersteAntwort"+zahl;
            String secondAnswer = "zweiteAntwort"+zahl;
            String thirdAnswer = "dritteAntwort"+zahl;
            tw.setText(R.string.question);
            Chk1.setText(R.string.firstAnswer);
            Chk1.setText(R.string.secondAnswer);
            Chk1.setText(R.string.thirdAnswer);
                    }});    
}   
3
  • 1
    instead of using strings.xml, you'd better use a db... so your answers will have a relation to your questions. Just randomly select a question and its related answers. Very easy. Commented Feb 23, 2014 at 16:24
  • i don´t know how to make it with a db so it would be much quicker if i get this code working. it should be easier in this way but i dont know why the errors appaer question, firstanswer, secondanswer, thirdanser cannot be resolved or is not a field Commented Feb 23, 2014 at 16:58
  • Your method would require creating a huge array, one for each question. Expanding it would require code and xml values. It might be easier to learn databases, which is the right solution. Commented Feb 23, 2014 at 19:07

1 Answer 1

1

First of all, if you can, write the data in an appropriate format. XML would be a good choice, you could create your own XML format and read it. An example XML format could be as follows, Android does include some excellent parsers as well.

<questions_list>
    <question name="Question 1?">
        <answer name="Answer 1"/>
        <answer name="Answer 2">
            <correct/>
        </answer>
        <answer name="Answer 3"/>
    </question>
</question_list>

If that doesn't work, maybe a String Array could work, like this:

<string-array name="question1">
    <item>Question 1</item>
    <item>Answer 1</item>
    ...
</string-array>

A third option would be to put the data into a SQLite database, probably with 5 columns, question, 3 answers, and the correct answer. There's a lot out there on how to do this, the easiest way is to use a language like Python to populate the database, then use something like SQLiteAssetHelper to put the database in to your program.

I think re-formatting will save you considerable work. XML, a database, or something else, but I'm sure a better format would help you considerably.

As it stands, you would have to reference not only the questions directly in the code, but also the answers. The tool simply isn't meant to be used the way you're using it.

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

1 Comment

That's a good alternative to using a db, since you can have indexed elements. +1

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.