Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

java - Android quiz How to show the question based on the answer?

I am currently doing a quiz app and the question should be coming out based on the answer. The below is my code. I have been looking online to try various options and cannot find a way to resolve it without making lots of errors. The code for my quiz activity is below. Thanks.

quiz.java

public class quiz extends AppCompatActivity implements View.OnClickListener {

    TextView questionNo, question;
    RadioGroup rg;
    Button quiz_nextBtn, quiz_PrevBtn;
    RadioButton rb1, rb2;
    int current_question = 0;
    int next_question=0;

    private quizAdapter[] questionBank = new quizAdapter[]{
            new quizAdapter(R.string.q1, R.string.q1_a1, R.string.q1_a2),
            new quizAdapter(R.string.q2, R.string.q2_a1, R.string.q2_a2),
            new quizAdapter(R.string.q3, R.string.q3_a1, R.string.q3_a2),
            new quizAdapter(R.string.q4, R.string.q4_a1, R.string.q4_a2),
            new quizAdapter(R.string.q5, R.string.q5_a1, R.string.q5_a2),
            new quizAdapter(R.string.q6, R.string.q6_a1, R.string.q6_a2),
            new quizAdapter(R.string.q7, R.string.q7_a1, R.string.q7_a2)
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        rg = findViewById(R.id.quiz_answer);
        rb1 = findViewById(R.id.rb1);
        rb2 = findViewById(R.id.rb2);
        questionNo = findViewById(R.id.quiz_questionNo);
        question = findViewById(R.id.quiz_question);
        quiz_nextBtn = findViewById(R.id.quiz_nextBtn);
        quiz_PrevBtn = findViewById(R.id.quiz_PrevBtn);

        quiz_nextBtn.setOnClickListener(this);
        quiz_PrevBtn.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()){
            case R.id.quiz_nextBtn:
                if(current_question<7){
                    current_question++;
                    if(current_question == 6){
                        question.setText("We are finish");
                    }
                    else {
                        if(current_question == 0 && rb1.isChecked()){
                            current_question = 1;
                        }
                        else if (current_question == 0 && rb2.isChecked()){
                            current_question = 4;
                        }
                        else if (current_question == 1 && rb1.isChecked()){
                            current_question = 2;
                        }
                        else if (current_question == 1 && rb2.isChecked()){
                            current_question = 3;
                        }
                        else if (current_question == 4 && rb1.isChecked()){
                            current_question = 5;
                        }
                        else if (current_question == 4 && rb2.isChecked()){
                            current_question = 6;
                        }

                        updateQuestion();
                    }

                }
                break;
            case R.id.quiz_PrevBtn:
                if (current_question > 0) {
                    current_question = (current_question - 1) % questionBank.length;
                    updateQuestion();
                }
        }
    }

    private void updateQuestion() {
        Log.d("Current", "onClick: " + current_question);

        question.setText(questionBank[current_question].getQuestion());
        rb1.setText(questionBank[current_question].getRb1());
        rb2.setText(questionBank[current_question].getRb2());
    }
}

quizAdapter.java

public class quizAdapter {

    private int question;
    private int rb1;
    private int rb2;

    public quizAdapter(int question, int rb1, int rb2)
    {
        this.question = question;
        this.rb1 = rb1;
        this.rb2 = rb2;
    }

    public int getQuestion()
    {
        return question;
    }

    public int getRb1()
    {
        return rb1;
    }

    public int getRb2()
    {
        return rb2;
    }

}

question from:https://stackoverflow.com/questions/65871541/android-quiz-how-to-show-the-question-based-on-the-answer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think a better way would be to use a HashMap and set the quiz answers and questions as key value pairs, for what your trying to implement you will have multiple keys (the value of the current question and option chosen) for a single value(the next question to be shown). one way to do it would be to create a custom HashMap like below

 import java.util.HashMap;
import java.util.Map;
 
class Key<K1, K2> {
    public K1 key1;
    public K2 key2;
 
    public Key(K1 key1, K2 key2) {
        this.key1 = key1;
        this.key2 = key2;
    }
 
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
 
        Key key = (Key) o;
 
        if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) return false;
        if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) return false;
 
        return true;
    }
 
    @Override
    public int hashCode() {
        int result = key1 != null ? key1.hashCode() : 0;
        result = 31 * result + (key2 != null ? key2.hashCode() : 0);
        return result;
    }
 
    @Override
    public String toString() {
        return "[" + key1 + ", " + key2 + "]";
    }
}
 
class Main
{
    public static void main(String[] args) {
        //  Create a HashMap with Key as key
        Map<Key, String> multiKeyMap = new HashMap<>();
 
        // [key1, key2] -> value1
        Key k12 = new Key("key1", "key2");
        multiKeyMap.put(k12, "value1");
 
        // [key3, key4] -> value2
        Key k34 = new Key("key3", "key4");
        multiKeyMap.put(k34, "value2");
 
        // print multi-key map
        System.out.println(multiKeyMap);
 
        // print value corresponding to key1 and key2
        System.out.println(multiKeyMap.get(k12));
    }
}

more explanation can be found in this link https://www.techiedelight.com/implement-map-with-multiple-keys-multikeymap-java/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...