简体   繁体   中英

How to update the text of radio buttons with strings.xml

I build an application which asks the user several questions. Each question has 4 answers (radio buttons) which the user need to choose. I want to build one activity which updates with the questions and answers.

All the questions and answers are written in strings.xml :

 <!-- Q1 -->
    <string name="Q1">Q1</string>
    <string name="Q1A1">answer 1</string>
    <string name="Q1A2">answer 2</string>
    <string name="Q1A3">answer 3</string>
    <string name="Q1A4">answer 4</string>

    <!-- Q2 -->
    <string name="Q2">Q2</string>
    <string name="Q2A1">answer 1</string>
    <string name="Q2A2">answer 2</string>
    <string name="Q2A3">answer 3</string>
    <string name="Q2A4">answer 4</string>

....

(I want to be able to add more questions in the future via this file and not in hard coded way)

When the user choose to get to the next question, I want to update the questions and answers via dynamic way:

private void updateQuestion(int currentQuestion) {

        // Update Questions
        TextView textView = (TextView)findViewById(R.id.QuestionLabel);
        textView.setText(getResources().getString(R.string.Q1)); // ??? how to get string Q#currentQuestion

        // Update Answares
        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rgQuestions);
        for (int i = 0; i < radioGroup .getChildCount(); i++)
        {
            // ??? how to get string of Q'currentQuestion'A'i' ????
            ((RadioButton) radioGroup.getChildAt(i)).setText(getResources().getString(R.string.Q1A1));
        }
    } 

But how can I set the text of the radio buttons (and textView) via strings.xml by choosing the right string value ? (I want to build the StringRes with currentQuestion and i iterator). Is it possible?

You can use String Array :

<string-array name="question_array">
        <item>Q1</item>
        <item>Q2</item>
</string-array>

<string-array name="question_1_answer">
        <item>answer 1</item>
        <item>answer 2</item>
        <item>answer 3</item>
        <item>answer 4</item>
</string-array>

<string-array name="question_2_answer">
        <item>answer 1</item>
        <item>answer 2</item>
        <item>answer 3</item>
        <item>answer 4</item>
</string-array>

And access it via:

String[] questions = getResources().getStringArray(R.array.question_array);
//Call as per your convenience
updateQuestion(1, R.array.question_1_answer);
//updateQuestion(2, R.array.question_1_answer);

In your example:

    private void updateQuestion(int currentQuestion, int answerResId) {
            TextView textView = (TextView)findViewById(R.id.QuestionLabel);
            textView.setText(questions[currentQuestion]);     

            RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rgQuestions);
            String[] answers = getResources().getStringArray(answerResId);
            for (int i = 0; i < radioGroup .getChildCount(); i++)          {
                ((RadioButton) radioGroup.getChildAt(i)).setText(answers[i]);
            }

        } 

So basically above solution is just idea to your question, hope it will help you.

Try to use getIdentifier(), may be something like this:

    private void updateQuestion(int currentQuestion) {
    // Update Questions
    String questionString = "Q" + currentQuestion;
    TextView textView = (TextView)findViewById(R.id.QuestionLabel);
    textView.setText(getString(getResources().getIdentifier(questionString, "string", this.getPackageName())));

    // Update Answers
    String answerString;
    RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rgQuestions);
    for (int i = 0; i < radioGroup .getChildCount(); i++)
    {
        answerString = questionString + "A" + i;
        ((RadioButton) radioGroup.getChildAt(i)).setText(getString(getResources().
                getIdentifier(answerString, "string", this.getPackageName())));
    }
}

Hope it help :)

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