带有多个答案的Arraylist Android Studio测验应用

时间:2018-12-01 08:30:37

标签: java android arrays android-studio

我正在制作测验应用程序,并使用ArrayList。我的答案有问题:当我只有一个答案时,它就可以工作,但是如果这个问题有两个答案,该怎么办?

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class MainActivity extends AppCompatActivity {

    TextView questionLabel, questionCountLabel, scoreLabel;
    EditText answerEdt;
    Button submitButton;
    ProgressBar progressBar;
    ArrayList<QuestionModel> questionModelArraylist;


    int currentPosition = 0;
    int numberOfCorrectAnswer = 0;

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


        questionCountLabel = findViewById(R.id.noQuestion);
        questionLabel = findViewById(R.id.question);
        scoreLabel = findViewById(R.id.score);

        answerEdt = findViewById(R.id.answer);
        submitButton = findViewById(R.id.submit);
        progressBar = findViewById(R.id.progress);

        questionModelArraylist = new ArrayList<>();

        setUpQuestion();

        setData();

        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                checkAnswer();
            }
        });

        answerEdt.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                Log.e("event.getAction()",event.getAction()+"");
                Log.e("event.keyCode()",keyCode+"");
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                        (keyCode == KeyEvent.KEYCODE_ENTER)) {

                    checkAnswer();
                    return true;
                }
                return false;
            }
        });

    }
    public void checkAnswer(){
        String answerString  = answerEdt.getText().toString().trim();




        if(answerString.equalsIgnoreCase(questionModelArraylist.get(currentPosition).getAnswer())){
            numberOfCorrectAnswer ++;



            new SweetAlertDialog(MainActivity.this, SweetAlertDialog.SUCCESS_TYPE)
                    .setTitleText("Sehr gut!")
                    .setContentText("Richtig!")
                    .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            currentPosition ++;

                            setData();
                            answerEdt.setText("");
                            sweetAlertDialog.dismiss();
                        }
                    })
                    .show();


        }else {

            new SweetAlertDialog(MainActivity.this, SweetAlertDialog.ERROR_TYPE)
                    .setTitleText("Falsch :(")
                    .setContentText("Die Richtige Antwort ist : "+questionModelArraylist.get(currentPosition).getAnswer())
                    .setConfirmText("OK")
                    .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sDialog) {
                            sDialog.dismiss();

                            currentPosition ++;

                            setData();
                            answerEdt.setText("");
                        }
                    })
                    .show();
        }





        int x = ((currentPosition+1) * 100) / questionModelArraylist.size();

        progressBar.setProgress(x);



    }




    public void setUpQuestion(){


        questionModelArraylist.add(new QuestionModel("Write one planet located between Earth and the sun","Venus or Mercury"));

questionModelArraylist.add(new QuestionModel("the 5th planet from the sun","Jupiter"));

questionModelArraylist.add(new QuestionModel("write names of any two oceans","Atlantic Ocean or Arctic Ocean or Indian Ocean or Pacific Ocean orSouthern Ocean"));



    }

    public void setData(){


        if(questionModelArraylist.size()>currentPosition) {

            questionLabel.setText(questionModelArraylist.get(currentPosition).getQuestionString());

            scoreLabel.setText("Ergebnis :" + numberOfCorrectAnswer + "/" + questionModelArraylist.size());
            questionCountLabel.setText("Frage Nummer : " + (currentPosition + 1));


        }else{


            new SweetAlertDialog(MainActivity.this, SweetAlertDialog.SUCCESS_TYPE)
                    .setTitleText("Du bist Fertig :)")
                    .setContentText("dein Ergebnis ist : "+ numberOfCorrectAnswer + "/" + questionModelArraylist.size())
                    .setConfirmText("Wiederholen")
                    .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sDialog) {

                            sDialog.dismissWithAnimation();
                            currentPosition = 0;
                            numberOfCorrectAnswer = 0;
                            progressBar.setProgress(0);
                            setData();
                        }
                    })
                    .setCancelText("schließen")
                    .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sDialog) {

                            sDialog.dismissWithAnimation();
                            finish();
                        }
                    })
                    .show();

        }

    }



}

问题课

public class QuestionModel {

    public QuestionModel(String questionString, String answer) {
        QuestionString = questionString;
        Answer = answer;
    }

    public String getQuestionString() {
        return QuestionString;
    }

    public void setQuestionString(String questionString) {
        QuestionString = questionString;
    }

    public String getAnswer() {
        return Answer;
    }

    public void setAnswer(String answer) {
        Answer = answer;
    }

    private String QuestionString;
    private String Answer;


}

如您所见,我想在第一个问题中回答是水星还是金星,第二个问题是木星,而第3个问题有5个答案,“大西洋或北冰洋或印度洋或太平洋或南方海洋” 我该如何使其工作?谢谢。

2 个答案:

答案 0 :(得分:1)

基本上:通过更改数据模型。

您会看到,您创建了类来“模拟”现实。如果要允许多个答案,那么简单的一对一映射(就像QuestionModel暗示的那样)根本行不通。然后您的模型应该是1:n,就像1个问题字符串一样,但是答案可能是List<String>。对于只有一个答案的问题,该列表仅包含一个条目。

这就是去那里的方式:首先您先思考如何组织数据,然后构建所有内容,包括围绕它的UI结构。

答案 1 :(得分:0)

@GhostCat是正确的,必须在实施之前进行分析。对于您的特定情况,我认为枚举可能会有所帮助。我对您的目标并不十分清楚,但这就是我最初将如何实现的目标。

并在您的QuestionModel类中将其概括化,我将首先创建一个接口来描述任何可能的答案。

interface AnswerInterface {
    val value: String
}

我将使用泛型来扩展此接口,然后我的QuestionModel会变成这样。

class QuestionModel<QuestionAnswer : AnswerInterface>(
        var question: String,
        vararg answers: QuestionAnswer
)

QuestionAnswer可以是实现AnswerInterface的任何类,而varard意味着我可以具有任意数量的实例作为答案。

考虑到这一点,我的答案列举为:

enum class PlanetAnswers(override val value: String) : AnswerInterface {
    VENUS("Venus"),
    MERCURY("Mercury"),
    JUPITER("Jupiter")
}

最后,一切都像这样联系在一起:

val question1 = QuestionModel("One planet between Earth and Sun?", PlanetAnswers.VENUS, PlanetAnswers.MERCURY)
val question2 = QuestionModel("The 5th planet from the Sun?", PlanetAnswers.JUPITER)

我有一个问题和很多答案,但是它们都必须来自同一枚举。我可以创建更多的枚举,然后创建更多具有不同答案的问题。