Display messages when the answer is correct and incorrect

I am developing a quiz app. The test contains 1 question and 4 multiple choice answers.

When the user selects one of the 4 options, if his answer is correct, something like "Your answers are correct" should be displayed, otherwise "your answer is wrong" should be displayed along with the correct answer.

And there are also two buttons (next and back), one to go to the next question, the other to return.

Can anyone tell me how to write the code to go back to the previous question?

I did something like this:

 private void getShuffledArray() 
    {
        // TODO Auto-generated method stub
        for (int i = 1; i <= SIZE; i++) 
        {
            quizIndexList.add(i);

        }
        Collections.shuffle(quizIndexList);
        Log.d("ERR", "List A shuffling" + quizIndexList);

    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.button1:
            Log.d("ERR", v.getTag().toString());
            if (v.getTag().toString().equalsIgnoreCase("right")) {
                displayAnswer();


            }

            break;
        case R.id.button2:
            Log.d("ERR", v.getTag().toString());
            if (v.getTag().toString().equalsIgnoreCase("right")) {
                displayAnswer();
            }
            break;

        case R.id.button3:
            Log.d("ERR", v.getTag().toString());
            if (v.getTag().toString().equalsIgnoreCase("right")) {
                displayAnswer();
            }
            break;

        case R.id.button4:
            Log.d("ERR", v.getTag().toString());
            if (v.getTag().toString().equalsIgnoreCase("right")) {
                displayAnswer();
            }
            break;

        case R.id.btn_next:
//          lyt_ans.setVisibility(View.GONE);
//          lyt_quest.setVisibility(View.VISIBLE);
            counter += 1;
            if (counter >= SIZE) {
                Collections.shuffle(quizIndexList);
                counter = 0;
            }
            getInfoFromDB(quizIndexList.get(counter));
            reLoad();
            break;

        case R.id.btn_bck:
             btn_next.setOnClickListener(new OnClickListener() 
               {

                public void onClick(View v) 
                {
                    // TODO Auto-generated method stub

                    finish();
                }
               });

        }

      

}

Any help is noticeable and thanks you in advance.

+3


source to share


3 answers


@Rithesh - not get your code, but simple logic to do this, as follows What you need 1) an array of questions 2) Arraylist answers (or say two dimensional array) something like ArrayList<ArrayList<String>>

3) The index of answers for each question (it must match the index of the question 4) View with text view (To show the question), RadioGroup, which consists of 4 radio buttons, so it will only select one at a time 5) Two next and previous buttons 6) Main, addsetOnCheckedChangeListener

to its radio group, it will light up only when the user chooses any answer, then checks which radio button is set (for that set switch tag to its position), so suppose the question is not 2 and radio position is 3 checked, then just check your array of answers to no answer and if it matches you can show the toast and if it doesn't match find the Answers answer from Answers and show it in the toast or alert. 7) now when you click on the next button, change the text in plain text to the next question and the radio button text to the next set of answers and vice versa for the previous button click

This logically doesn't give you the code

Parameter setting code



RadioButton option_rdb1 = (RadioButton) findViewById(R.id.option_rdb1);
setOptions(0);

// if you used ArrayList<ArrayList<String>> optionslist
public void setOptions(int index){
if((index-1) <= optionsList.size()){
    ArrayList<String> temp = optionslist.get(index);
    options_rdb1.setText(temp.get(0).toString();
    options_rdb1.setText(temp.get(0).toString();
    options_rdb1.setText(temp.get(0).toString();
    options_rdb1.setText(temp.get(0).toString();
  }
}

      

just call setOptions in the next previous one with the question pointer. I used index-1

when I started arraylist at 0. So it is up to you how you use the index. This way you can set the first parameters. Also in your function that you will call on the next button, click the button above the line of code to set the next

+3


source


In the onclick of the btn_next button enter int prev

now before changing the counter.

prev=counter<--- store the counter value..
counter += 1;
        if (counter >= SIZE) {
            Collections.shuffle(quizIndexList);
            counter = 0;}

      



and in onclick btn_bck get the previous question like this.

     getInfoFromDB(quizIndexList.get(prev));
    reLoad();

      

+1


source


  • you only need to shuffle once (for example in oncreate) and by pressing the Next or Back key you have to increase or decrease the index of the question array
0


source







All Articles