多个类别测验应用

时间:2017-09-19 14:12:49

标签: java android

我正在制作一个有关不同国家/地区的问题的测验应用。我已经让它适用于一个国家,但现在我想让它适用于所有国家,但我不知道如何。我做了一个问题库和一个quizactivity,但我现在不知道如何进行,所以我希望有人可以帮助我。

这是我的问答:

package com.example.quizapp;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends AppCompatActivity {

private QuestionLibrary mQuestionLibrary = new QuestionLibrary();

private TextView mScoreView;
private TextView mQuestionView;
private Button mButtonChoice1;
private Button mButtonChoice2;
private Button mButtonChoice3;
private Button mButtonChoice4;

private String mAnswerFrankrijk;
private int mScoreFrankrijk = 0;
private int mQuestionNumber = 0;

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

    mScoreView = (TextView)findViewById(R.id.score1);
    mQuestionView = (TextView)findViewById(R.id.question1);
    mButtonChoice1 = (Button)findViewById(R.id.choice1);
    mButtonChoice2 = (Button)findViewById(R.id.choice2);
    mButtonChoice3 = (Button)findViewById(R.id.choice3);
    mButtonChoice4 = (Button)findViewById(R.id.choice4);

    updateQuestion();

    //Start of Button Listener for Button1
    mButtonChoice1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            //My logic for Button goes in here

            if (mButtonChoice1.getText() == mAnswerFrankrijk){
                mScoreFrankrijk = mScoreFrankrijk + 1;
                updateScore(mScoreFrankrijk);
                updateQuestion();
                //This line of code is optiona
                Toast.makeText(QuizActivity.this, "Goed", Toast.LENGTH_SHORT).show();

            }else {
                Toast.makeText(QuizActivity.this, "Fout", Toast.LENGTH_SHORT).show();
                updateQuestion();
            }
        }
    });

    //End of Button Listener for Button1

    //Start of Button Listener for Button2
    mButtonChoice2.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            //My logic for Button goes in here

            if (mButtonChoice2.getText() == mAnswerFrankrijk){
                mScoreFrankrijk = mScoreFrankrijk + 1;
                updateScore(mScoreFrankrijk);
                updateQuestion();
                //This line of code is optiona
                Toast.makeText(QuizActivity.this, "Goed", Toast.LENGTH_SHORT).show();

            }else {
                Toast.makeText(QuizActivity.this, "Fout", Toast.LENGTH_SHORT).show();
                updateQuestion();
            }
        }
    });

    //End of Button Listener for Button2


    //Start of Button Listener for Button3
    mButtonChoice3.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            //My logic for Button goes in here

            if (mButtonChoice3.getText() == mAnswerFrankrijk){
                mScoreFrankrijk = mScoreFrankrijk + 1;
                updateScore(mScoreFrankrijk);
                updateQuestion();
                //This line of code is optiona
                Toast.makeText(QuizActivity.this, "Goed", Toast.LENGTH_SHORT).show();

            }else {
                Toast.makeText(QuizActivity.this, "Fout", Toast.LENGTH_SHORT).show();
                updateQuestion();
            }
        }
    });

    //End of Button Listener for Button3

    //Start of Button Listener for Button3
    mButtonChoice4.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            //My logic for Button goes in here

            if (mButtonChoice4.getText() == mAnswerFrankrijk){
                mScoreFrankrijk = mScoreFrankrijk + 1;
                updateScore(mScoreFrankrijk);
                updateQuestion();
                //This line of code is optional
                Toast.makeText(QuizActivity.this, "Goed", Toast.LENGTH_SHORT).show();

            }else {
                Toast.makeText(QuizActivity.this, "Fout", Toast.LENGTH_SHORT).show();
                updateQuestion();
            }
        }
    });

    //End of Button Listener for Button3




}

private void updateQuestion(){
    mQuestionView.setText(mQuestionLibrary.getQuestionFrankrijk(mQuestionNumber));
    mButtonChoice1.setText(mQuestionLibrary.getChoice1Frankrijk(mQuestionNumber));
    mButtonChoice2.setText(mQuestionLibrary.getChoice2Frankrijk(mQuestionNumber));
    mButtonChoice3.setText(mQuestionLibrary.getChoice3Frankrijk(mQuestionNumber));
    mButtonChoice4.setText(mQuestionLibrary.getChoice4Frankrijk(mQuestionNumber));

    mAnswerFrankrijk = mQuestionLibrary.getCorrectAnswerFrankrijk(mQuestionNumber);
    mQuestionNumber++;
}


private void updateScore(int point) {mScoreView.setText("" + mScoreFrankrijk);
}



}

这是我的问题库:

package com.example.quizapp;

public class QuestionLibrary {

private String mQuestionsFrankrijk [] = {
        "Wat is de hoofdstad van Frankrijk?",
        "Wat is de bijnaam van het Franse nationale voetbalelftal",
        "Welke van de volgende landen grenst niet aan Frankrijk?",
        "Bij welke sport hoort 'le maillot jaune'?",
        "Welk museum in Parijs heeft een piramide als ingang?"
};

private String mChoicesFrankrijk [][] = {
        {"Lyon", "Parijs", "Nice", "Bordeaux"},
        {"La France", "Le Coq Sportif", "Les Bleus", "Les Gagnants"},
        {"Zwitserland", "België", "Spanje", "Oostenrijk"},
        {"Tennis", "Wielrennen", "Rugby", "Cricket"},
        {"Musée d'Orsay", "Musée Rodin", "Louvre", "Centre Georges Pompidou"},

};

private String mCorrectAnswers [] = {"Parijs", "Les Bleus", "Oostenrijk", "Wielrennen", "Louvre"};



public String getQuestionFrankrijk(int a) {
    String question = mQuestionsFrankrijk[a];
    return question;
}

public String getChoice1Frankrijk(int a) {
    String choice0 = mChoicesFrankrijk[a][0];
    return choice0;
}

public String getChoice2Frankrijk(int a) {
    String choice1 = mChoicesFrankrijk[a][1];
    return choice1;
}
public String getChoice3Frankrijk(int a) {
    String choice2 = mChoicesFrankrijk[a][2];
    return choice2;
}
public String getChoice4Frankrijk(int a) {
    String choice3 = mChoicesFrankrijk[a][3];
    return choice3;
}

public String getCorrectAnswerFrankrijk(int a) {
    String answer = mCorrectAnswers[a];
    return answer;
}
}

1 个答案:

答案 0 :(得分:1)

我能想到的最简单的解决方案是使QuestionLibrary成为一个接口或抽象类。然后实现具有特定国家/地区问题的Country1QuestionLibrary和具有另一组问题的Country2QuestionLibrary。然后,您可以通过执行

动态交换出呈现给用户的一组问题

questionLibrary = new Country1QuestionLibrary();

然后您需要一种方法让您的用户更改国家/地区。在用户界面上,添加另一个“更改国家/地区”按钮,与其他按钮的工作方式类似。在onClickListener中,将QuestionLibrary的其他实现分配给可用的问题,并刷新问题和答案视图。

与大多数编程一样,有很多方法可以实现这一点。如果您可以实现我正在展示的路径,请在看到此解决方案的垮台或限制后,花一些时间尝试自己提出不同的解决方案。