如何在Android测验应用中跟踪分数

时间:2016-07-04 16:12:02

标签: java android

我想为每个回答的问题注册正确的答案,在点击其中一个备选项后转到下一个问题。我还想在另一个xml文件上显示正确答案的数量,该文件仅包含在最后一个问题被解答后立即保留分数的文本视图

我有一张旗帜和一个带有问题的文字视图,下面有4个按钮和替代品。

这是包含问题的问题类,pictureID(用于测验中的国家标志),选项a,b,c和正确答案。我也持有得分。

public class Question {

    private int bildID;
    private String question;
    private String optA;
    private String optB;
    private String optC;
    private String answer;
    private String score;

    //Constructor

    public Question(int bildID, String question, String optA, String optB, String optC, String answer) {
        this.bildID = bildID;
        this.question = question;
        this.optA = optA;
        this.optB = optB;
        this.optC = optC;
        this.answer = answer;

    } //End konstruktor

    //Get & Set method starts
    public int getBildID() {
        return bildID;
    }

    public void setBildID(int bildID) {
        this.bildID = bildID;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getOptA() {
        return optA;
    }

    public void setOptA(String optA) {
        this.optA = optA;
    }

    public String getOptB() {
        return optB;
    }

    public void setOptB(String optB) {
        this.optB = optB;
    }

    public String getOptC() {
        return optC;
    }

    public void setOptC(String optC) {
        this.optC = optC;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }
    //Get & Set methods end

} //End konstruktor

这是Mainactivity。

public class MainActivity extends AppCompatActivity {

    //Method that's gonna help to check index position and avoid outofbounds.
    private void fixIndexCountValue() {
        if (idx >= questionArray.size() - 1) {
            idx = questionArray.size() - 1;
        } else if (idx < 0) {
            idx = 0;
        }
    }


    //Creates an arraylist thats gonna hold questions, options, answers, pictureID.
    ArrayList<Question> questionArray = new ArrayList<>();
    //Arraylist thats gonna hold the buttons
    ArrayList<Button> buttons = new ArrayList<>();
    //Variable for index position
    int idx = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Arraylists
        questionArray = new ArrayList<>();
        buttons = new ArrayList<>();
        //End

        //CODE FOR QUESTIONS
        Question q1 = new Question(R.drawable.australien, "What is the capital of Australia?", "Melbourne", "Chicago", "Sydney", "Canberra");
        Question q2 = new Question(R.drawable.finland, "What country does this flag belong to?", "Sweden", "Denmark", "Norway", "Finland");
        Question q3 = new Question(R.drawable.frankrike, "What is the biggest city in France?", "Lyon", "Marseille", "Bordeaux", "Paris");
        Question q4 = new Question(R.drawable.jamaika, "What country does this flag belong to?", "Mexico", "Dominican Republic", "Cuba", "Jamaica");
        Question q5 = new Question(R.drawable.kina, "What is the capital of China?", "Shanghai", "Hangzhou", "Guangzhou", "Beijing");
        Question q6 = new Question(R.drawable.kurdistan, "What is the capital of Kurdistan?", "Kirkuk", "Dohuk", "Zakho", "Erbil");
        Question q7 = new Question(R.drawable.sverige, "What is the capital of Sweden?", "Gothenburg", "Malmö", "Helsinki", "Stockholm");
        Question q8 = new Question(R.drawable.norge, "What is the capital of Norway?", "Bergen", "Harstad", "Stockholm", "Oslo");
        Question q9 = new Question(R.drawable.ryssland, "What city in Russia has the highest population?", "Saint Petersburg", "Omsk", "Samara", "Moskva");
        Question q10 = new Question(R.drawable.tyskland, "What country does this flag belong to?", "Belgia", "Italy", "Austria", "Germany");

        questionArray.add(q1);
        questionArray.add(q2);
        questionArray.add(q3);
        questionArray.add(q4);
        questionArray.add(q5);
        questionArray.add(q6);
        questionArray.add(q7);
        questionArray.add(q8);
        questionArray.add(q9);
        questionArray.add(q10);

        final Question question = questionArray.get(idx);
        //END CODE FOR QUESTIONS


        // Code that's gonna fill buttons, tv and imageview with data.
        final ImageView bild = (ImageView) findViewById(R.id.imageView);
        final TextView tv_question = (TextView) findViewById(R.id.tv_question);
        final Button btnAlt1 = (Button) findViewById(R.id.btnAlt1);
        final Button btnAlt2 = (Button) findViewById(R.id.btnAlt2);
        final Button btnAlt3 = (Button) findViewById(R.id.btnAlt3);
        final Button btnAlt4 = (Button) findViewById(R.id.btnAlt4);
        //Textview on the scorescreen thats going to display score
        final TextView tvscore = (TextView) findViewById(R.id.tvscore);
        //KNAPP SOM KOMMER UPP PÅ FÖRSTA SIDAN, STARTA QUIZ
        final Button btnStart = (Button) findViewById(R.id.btnStart);

        //Adds data from the Question class with get methods
        tv_question.setText(question.getQuestion());
        bild.setImageResource(question.getBildID());
        btnAlt1.setText(question.getOptA());
        btnAlt2.setText(question.getOptB());
        btnAlt3.setText(question.getOptC());
        btnAlt4.setText(question.getAnswer());
        //END

        //Adds the button id in the arraylist
        buttons.add(btnAlt1);
        buttons.add(btnAlt2);
        buttons.add(btnAlt3);
        buttons.add(btnAlt4);
        //End


        //CODE FOR CLICK FUNCTION ON EACH BUTTON
        btnAlt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                idx++;
                fixIndexCountValue();
                Question questionClick = questionArray.get(idx);
                tv_question.setText(questionClick.getQuestion());
                bild.setImageResource(questionClick.getBildID());
                Collections.shuffle(buttons);
                buttons.get(0).setText(questionClick.getOptA());
                buttons.get(1).setText(questionClick.getOptB());
                buttons.get(2).setText(questionClick.getOptC());
                buttons.get(3).setText(questionClick.getAnswer());
            }
        }); //END BUTTON 1

        btnAlt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                idx++;
                fixIndexCountValue();
                Question questionClick = questionArray.get(idx);
                tv_question.setText(questionClick.getQuestion());
                bild.setImageResource(questionClick.getBildID());
                Collections.shuffle(buttons);
                buttons.get(0).setText(questionClick.getOptA());
                buttons.get(1).setText(questionClick.getOptB());
                buttons.get(2).setText(questionClick.getOptC());
                buttons.get(3).setText(questionClick.getAnswer());
            }
        }); //END BUTTON 2

        btnAlt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                idx++;
                fixIndexCountValue();
                Question questionClick = questionArray.get(idx);
                tv_question.setText(questionClick.getQuestion());
                bild.setImageResource(questionClick.getBildID());
                Collections.shuffle(buttons);
                buttons.get(0).setText(questionClick.getOptA());
                buttons.get(1).setText(questionClick.getOptB());
                buttons.get(2).setText(questionClick.getOptC());
                buttons.get(3).setText(questionClick.getAnswer());
            }
        }); //END BUTTON 3

        btnAlt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                idx++;
                fixIndexCountValue();
                Question questionClick = questionArray.get(idx);
                tv_question.setText(questionClick.getQuestion());
                bild.setImageResource(questionClick.getBildID());
                Collections.shuffle(buttons);
                buttons.get(0).setText(questionClick.getOptA());
                buttons.get(1).setText(questionClick.getOptB());
                buttons.get(2).setText(questionClick.getOptC());
                buttons.get(3).setText(questionClick.getAnswer());
            }
        });//END BUTTON 4

    } //End oncreate

} //End main

1 个答案:

答案 0 :(得分:0)

here

下载更新后的项目

我在您的MainActivity中添加了此方法:

private void showNextQuestionAndSaveAnswer(String answer)
{

    if (answer == null)
        return;

    if (questionArray.get(idx).getAnswer().equalsIgnoreCase(answer))
    {
        questionArray.get(idx).isCorrect = true;
    }

    idx++;

    if (idx < questionArray.size()) {
        fixIndexCountValue();
        Question questionClick = questionArray.get(idx);
        tv_question.setText(questionClick.getQuestion());
        bild.setImageResource(questionClick.getBildID());
        Collections.shuffle(buttons);
        buttons.get(0).setText(questionClick.getOptA());
        buttons.get(1).setText(questionClick.getOptB());
        buttons.get(2).setText(questionClick.getOptC());
        buttons.get(3).setText(questionClick.getAnswer());
    }
    else
    {
        Intent resultIntent = new Intent(MainActivity.this,ResultActivity.class);
        resultIntent.putExtra("list",questionArray);
        startActivity(resultIntent);
    }
}