0

I'm creating a quiz app, but I'm not able to get previous question pressing the back Button.
I get the questions from sqlite db.

Please help me with the code.

My code is working fine for the next Button, but not working for the back Button.

public class MainActivity extends AppCompatActivity {
         List<Questions> quesList;
         int score=0;
         int qid=0;
        Questions currentQ;
        TextView tv;
        RadioButton rb1,rb2,rb3,rb4;
        ImageButton next,back;
        Questions cur;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            DbHelper db=new DbHelper(this);
            final RadioGroup grp=(RadioGroup) findViewById(R.id.radiogroup1);
            quesList=db.getAllQuestions();
            if(quesList!= null && quesList.size() !=0) {
                currentQ=quesList.get(qid);
            }
            tv=(TextView) findViewById(R.id.tv1);
            rb1=(RadioButton) findViewById(R.id.radio1);
            rb2=(RadioButton) findViewById(R.id.radio2);
            rb3=(RadioButton) findViewById(R.id.radio3);
            rb4=(RadioButton) findViewById(R.id.radio4);
            next=(ImageButton) findViewById(R.id.forward);
            back=(ImageButton) findViewById(R.id.backward);
            setQuestionView();

  //code for next button..      

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


                RadioButton answer=(RadioButton) findViewById(grp.getCheckedRadioButtonId());

                if(currentQ.getAnswer().equals(answer.getText()))
                {
                    score++;
                    Log.d("score", "Your score" + score);
                }
                if(qid<5){
                    currentQ=quesList.get(qid);
                    setQuestionView();

                    qid++;
                    grp.clearCheck();
                }else{
                    Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                    Bundle b = new Bundle();
                    b.putInt("score", score); //Your score
                    intent.putExtras(b); //Put your score to your next Intent
                    startActivity(intent);
                    finish();
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              if(qid>0) {
            qid--; // here you decrement
            currentQ=quesList.get(qid);  // here you load
            setQuestionView();
            grp.clearCheck();
       } else {

       }
    }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    private void setQuestionView()
    {
        tv.setText(currentQ.getQuestion());
        rb1.setText(currentQ.getOption1());
        rb2.setText(currentQ.getOption2());
        rb3.setText(currentQ.getOption3());
        rb4.setText(currentQ.getOption4());

    }

}

2 Answers 2

1

You need to override Activity.onBackPressed(), so the app will know what you want it to do when you press back. Like this:

@Override
public void onBackPressed()
{
     showPreviousQuestion();
}

In your case, I think you want to do something like:

@Override
public void onBackPressed()
{
    if(qid>0) {
        qid--; // here you decrement
        currentQ=quesList.get(qid);  // here you load
        setQuestionView();
        grp.clearCheck();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

when we click on prev button,we should get the question with checked answer...In the above code responses are erased.if i want to change my answer i should be able to modify my option and it should affect my score.
you should implement some logic that will track the answers and allow you to modify them. this code is an example of how to change the functionality of the back button
0

Call the finish method of activity

 back.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {
              if(qid>0) {
            qid--; // here you decrement
            currentQ=quesList.get(qid);  // here you load
            setQuestionView(); 
            grp.clearCheck();
       } else { 
finish();

       } 
    } 
        }); 

1 Comment

when we click on prev button,we should get the question with checked answer...In the above code responses are erased.if i want to change my answer i should be able to modify my option and it should affect my score.

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.