2

I am making a math quiz where a mathematical question will be asked in the form of a formula. I put my questions in an ArrayList:

public class DEasy extends AppCompatActivity {

private TextView countLabel;
private TextView questionLabel;
private Button answerBtn1;
private Button answerBtn2;
private Button answerBtn3;
private Button answerBtn4;

private String rightAnswer;
private int rightAnswerCount = 0;
private int quizCount = 1;
static final private int QUIZ_COUNT = 10;

ArrayList<ArrayList<String>> quizArray = new ArrayList<>();
String quizData[][] = {
        {"x", "1", "0", "x", "-1"},
        {"x²", "2x", "x", "2/x²", "2x²"},
        {"64", "0", "1", "64", "8"},
        {"x² + 5x", "2x + 5", "7x", "2x", "½x + 5"},
        {"19x", "19", "x", "0", "x + 19"},
        {"642", "34", "97", "5x-2", "1"}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_deasy);

    countLabel = (TextView) findViewById(R.id.countLabel);
    questionLabel = (TextView) findViewById(R.id.questionLabel);
    answerBtn1 = (Button) findViewById(R.id.answerBtn1);
    answerBtn2 = (Button) findViewById(R.id.answerBtn2);
    answerBtn3 = (Button) findViewById(R.id.answerBtn3);
    answerBtn4 = (Button) findViewById(R.id.answerBtn4);

    for (int i = 0; i < quizData.length; i++) {

        ArrayList<String> tmpArray = new ArrayList<>();
        tmpArray.add(quizData[i][0]);
        tmpArray.add(quizData[i][1]);
        tmpArray.add(quizData[i][2]);
        tmpArray.add(quizData[i][3]);
        tmpArray.add(quizData[i][4]);

        quizArray.add(tmpArray);
    }
    showNextQuiz();
}
public void showNextQuiz() {
    countLabel.setText( getString(R.string.question) + " " + quizCount + ".");

    Random random = new Random();
    int randomNum = random.nextInt(quizArray.size());

    ArrayList<String> quiz = quizArray.get(randomNum);

    questionLabel.setText(quiz.get(0));
    rightAnswer = quiz.get(1);

    quiz.remove(0);
    Collections.shuffle(quiz);

    answerBtn1.setText(quiz.get(0));
    answerBtn2.setText(quiz.get(1));
    answerBtn3.setText(quiz.get(2));
    answerBtn4.setText(quiz.get(3));

    quizArray.remove(randomNum);
}
public void checkAnswer(View view){
    Button answerBtn = (Button) findViewById(view.getId());
    String btnText = answerBtn.getText().toString();

    String alertTitle;

    if (btnText.equals(rightAnswer)){
        alertTitle = "Correct";
        rightAnswerCount++;
    }
    else {
        alertTitle = "Wrong";
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(alertTitle);
    builder.setMessage("Answer: " + rightAnswer);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            if (quizCount == QUIZ_COUNT){
                Intent intent = new Intent(getApplicationContext(), DResult.class);
                intent.putExtra("RIGHT_ANSWER_COUNT", rightAnswerCount);
                startActivity(intent);

            }
            else{
                quizCount++;
                showNextQuiz();
            }
        }
    });
    builder.setCancelable(false);
    builder.show();
}

} As you can see I tried to make the formula x^2, this wil not be shown as x with a small exponent 2 but as x^2. This x^2 is not what I want. How can I used for example html in this arraylist to achieve this goal. Or is there another way?

Thanks aton!

1 Answer 1

1

Here in replacement of "^" we can use this: "∧".

So now replace code at where you accessing this string array in Adapter as:

  Html.fromHtml(quizArray[][])

Thanks and happy coding

EDITED:

Here change it as:

   questionLabel.setText(Htm.fromHtml(quiz.get(0)));

Just it will work

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks alot for helping me out. However, I am new to coding (started this week) and this piece is copied from a video. I myself am not able to fully create something this complex on my own yet. I do not know where I have to put the code you sugested me, Here is my entire java file
Where is java file
Thanks alot, I managed to implement this in my code succesfully and accepted the answer. I also asked another question on the forum. I would be so grateful if you could help me out once again stackoverflow.com/questions/47618227/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.