NivardJ
NivardJ

Reputation: 45

Multiple categories quiz app

I'm making a quiz app with question about different countries. I already made it work for one country, but now I'd like to make it work for all the countries, but I don't exactly know how. I've made a question library and a quizactivity, but I don't know how to proceed right now, so I hope someone can help me.

Here is my 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);
}



}

And here is my question library:

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;
}
}

Upvotes: 0

Views: 519

Answers (1)

JitterbugChew
JitterbugChew

Reputation: 401

The easiest solution I can think is to make QuestionLibrary an interface or abstract class. Then implement a Country1QuestionLibrary with a specific country's questions and Country2QuestionLibrary with another set of questions. Then you can dynamically swap out the set of questions being presented to the user by doing

questionLibrary = new Country1QuestionLibrary();

Then you need a way for your user to change the country. On the UI, add another button that's "Change Country" implemented similarly to how your other buttons are working. In the onClickListener, assign the other implementation of QuestionLibrary to the available questions and refresh the question and answers views.

As with most things programming, there are a bunch of ways to implement this. If you can implement the path I'm showing, take some time to try and come up with a different solution on your own after seeing the downfalls or restrictions of this solution.

Upvotes: 1

Related Questions