Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2. List<Questions> quesList;
  3. int score=0;
  4. int qid=0;
  5. Questions currentQ;
  6. TextView tv;
  7. RadioButton rb1,rb2,rb3,rb4;
  8. ImageButton next,back;
  9. Questions cur;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. DbHelper db=new DbHelper(this);
  15. final RadioGroup grp=(RadioGroup) findViewById(R.id.radiogroup1);
  16. quesList=db.getAllQuestions();
  17. if(quesList!= null && quesList.size() !=0) {
  18. currentQ=quesList.get(qid);
  19. }
  20. tv=(TextView) findViewById(R.id.tv1);
  21. rb1=(RadioButton) findViewById(R.id.radio1);
  22. rb2=(RadioButton) findViewById(R.id.radio2);
  23. rb3=(RadioButton) findViewById(R.id.radio3);
  24. rb4=(RadioButton) findViewById(R.id.radio4);
  25. next=(ImageButton) findViewById(R.id.forward);
  26. back=(ImageButton) findViewById(R.id.backward);
  27. setQuestionView();
  28.  
  29. //code for next button..
  30.  
  31. next.setOnClickListener(new View.OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34.  
  35.  
  36. RadioButton answer=(RadioButton) findViewById(grp.getCheckedRadioButtonId());
  37.  
  38. if(currentQ.getAnswer().equals(answer.getText()))
  39. {
  40. score++;
  41. Log.d("score", "Your score" + score);
  42. }
  43. if(qid<5){
  44. currentQ=quesList.get(qid);
  45. setQuestionView();
  46.  
  47. qid++;
  48. grp.clearCheck();
  49. }else{
  50. Intent intent = new Intent(MainActivity.this, ResultActivity.class);
  51. Bundle b = new Bundle();
  52. b.putInt("score", score); //Your score
  53. intent.putExtras(b); //Put your score to your next Intent
  54. startActivity(intent);
  55. finish();
  56. }
  57. }
  58. });
  59. back.setOnClickListener(new View.OnClickListener() {
  60. @Override
  61. public void onClick(View v) {
  62. if(qid>0) {
  63. qid--; // here you decrement
  64. currentQ=quesList.get(qid); // here you load
  65. setQuestionView();
  66. grp.clearCheck();
  67. } else {
  68.  
  69. }
  70. }
  71. });
  72. }
  73. @Override
  74. public boolean onCreateOptionsMenu(Menu menu) {
  75. // Inflate the menu; this adds items to the action bar if it is present.
  76. getMenuInflater().inflate(R.menu.menu_main, menu);
  77. return true;
  78. }
  79. private void setQuestionView()
  80. {
  81. tv.setText(currentQ.getQuestion());
  82. rb1.setText(currentQ.getOption1());
  83. rb2.setText(currentQ.getOption2());
  84. rb3.setText(currentQ.getOption3());
  85. rb4.setText(currentQ.getOption4());
  86.  
  87. }
  88.  
  89.  
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement