简体   繁体   中英

Displaying messages when the answer is right and wrong

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

When the user selects any one of the option out of 4, if its a right answer then something like "Your answers is right" should be displayed, else "Your answer is wrong" should be displayed along with the correct answer.

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

Can someone tell me how to write the code for going back to previous question?

I have done 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 appreciable and thank you in advance.

@Rithesh -- Not getting your code but simple logic for this as follows What you need 1) Question array 2) Answers Arraylist (Or say two dimensional array) something like ArrayList<ArrayList<String>> 3) Answer index for each question (It should match to question index 4) A view with a textview (To show question), RadioGroup which consist 4 Radio Buttons so that it will only select one at a time 5) Two buttons next & previous 6) Main thing Add setOnCheckedChangeListener to your radio group it will get fire only when user select any answer then check which radio button is checked (For this set tag of radio button to it's position) so suppose question no is 2 & radio checked position is 3 then just check in your answer array for answer no & if it matches you can show toast & if it's not match then find write answer from Answers arraylist & show it in toast or by alert. 7) now when you click on next button change text view's text to next question & radio button's text to next set of answer & vice versa for previous button click

This is logical cant provide you code

Code to set options

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 next previous with index of question. I used index-1 as arraylist start with 0 So it depends on you how you use index. This way you can set first options Also in you function which you will call on next button click use above line of code to set next

In onclick of the button btn_next put an int prev

now before changing counter..

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

and in onclick of btn_bck get the prev question like this..

     getInfoFromDB(quizIndexList.get(prev));
    reLoad();
  1. you need to shuffle only once (in oncreate for example) and pressing next or back you should increment or decrement index of question array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM