Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. // QUESTION SCORE TEXT VIEWS
  2. private TextView question1_score, question2_score, question3_score, question4_score, question5_score, finalScoreTextView, nameFinalScoreTextView;
  3.  
  4. // TO STORE THE ANSWER AND MATCH VALUES FROM PREFERENCES FOR FINAL SCORE INT
  5. private int question1_answer_value, question2_answer_value, question3_answer_value, question4_answer_value, question5_answer_value, quizScoreInt, courtInt;
  6.  
  7. public int finalInt = 0;
  8.  
  9. // TO STORE THE NAME VALUE FROM PREFERENCES
  10. private String nameValue, playerNameToUse;
  11.  
  12. // FOR DISPLAYING THE SCORES
  13. private TextView quizScoreView, courtScoreView, finalScoreView, endMsgView;
  14.  
  15. // FOR EASIER ACCESS OF DATA
  16. private ArrayList<String> endMessages;
  17.  
  18. // TRY AGAIN BUTTON
  19. private Button tryAgainBtn;
  20.  
  21. SharedPreferences my_preferences;
  22.  
  23. // METHOD FOR RESTARTING THE WHOLE APPLICATION
  24. private void restartApp() {
  25. Intent reloadMainActivity = new Intent(this, MainActivity.class);
  26. startActivity(reloadMainActivity);
  27. }
  28.  
  29. @Override
  30. protected void onCreate(@Nullable Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.score_activity);
  33.  
  34.  
  35. // GETTING ACCESS TO WRITEN FILE VALUES FROM SHARED PREFERENCES
  36.  
  37. my_preferences = PreferenceManager.getDefaultSharedPreferences(this);
  38.  
  39.  
  40. // GETTING THE STORED VALUES FROM SHARED PREFERENCES
  41. courtInt = my_preferences.getInt("set_value_a", 0);
  42. nameValue = my_preferences.getString("name_value", "");
  43.  
  44. // FINDING THE TEXT VIEWVS TO DISPLAY THE SCORE
  45. question1_score = (TextView) findViewById(R.id.answer_value_1);
  46. question2_score = (TextView) findViewById(R.id.answer_value_2);
  47. question3_score = (TextView) findViewById(R.id.answer_value_3);
  48. question4_score = (TextView) findViewById(R.id.answer_value_4);
  49. question5_score = (TextView) findViewById(R.id.answer_value_5);
  50. finalScoreTextView = (TextView) findViewById(R.id.answer_value_final);
  51.  
  52. // FINDING THE TEXT VIEW TO DISPLAY PLAYERS NAME
  53. nameFinalScoreTextView = (TextView) findViewById(R.id.name_final_score);
  54.  
  55. // FINDING THE VIEWS TO DISPLAY SCORES AND MASSAGES
  56. quizScoreView = (TextView) findViewById(R.id.final_quiz_score_tV);
  57. courtScoreView = (TextView) findViewById(R.id.final_court_score_tV);
  58. finalScoreView = (TextView) findViewById(R.id.final_final_score_tv);
  59. endMsgView = (TextView) findViewById(R.id.end_msg_view);
  60.  
  61. // GETTING THE VALUES FOR CORRECT ANSWERS
  62. question1_answer_value = my_preferences.getInt("value1", 2);
  63. question2_answer_value = my_preferences.getInt("value2", 2);
  64. question3_answer_value = my_preferences.getInt("value3", 2);
  65. question4_answer_value = my_preferences.getInt("value4", 2);
  66. question5_answer_value = my_preferences.getInt("value5", 2);
  67.  
  68. // CALCULATING CORRECT AND INCORRECT ANSWERS
  69. quizScoreInt = question1_answer_value + question2_answer_value + question3_answer_value + question4_answer_value + question5_answer_value;
  70.  
  71. // SETTING THE FINAL SCORE
  72. if (my_preferences != null) {
  73. finalInt = quizScoreInt + courtInt;
  74. } else {
  75. finalInt = 1;
  76. }
  77.  
  78. // SETTING THE TEXT VALUES TO THE TEXT VIEWS
  79.  
  80.  
  81. quizScoreView.setText(quizScoreInt + "/5");
  82. courtScoreView.setText(courtInt + "/2");
  83. finalScoreView.setText(finalInt + "/7");
  84.  
  85.  
  86. // CHECKING THE ANSWER VALUES TO SET THE TEXT ON EACH QUESTION SCORE VIEW
  87. if (question1_answer_value == 1){
  88. question1_score.setText("Correct");
  89. } else {
  90. question1_score.setText("Incorrect");
  91. question1_score.setTextColor(Color.BLACK);
  92. }
  93.  
  94. if (question2_answer_value == 1){
  95. question2_score.setText("Correct");
  96. } else {
  97. question2_score.setText("Incorrect");
  98. question2_score.setTextColor(Color.BLACK);
  99. }
  100.  
  101. if (question3_answer_value == 1){
  102. question3_score.setText("Correct");
  103. } else {
  104. question3_score.setText("Incorrect");
  105. question3_score.setTextColor(Color.BLACK);
  106. }
  107.  
  108. if (question4_answer_value == 1){
  109. question4_score.setText("Correct");
  110. } else {
  111. question4_score.setText("Incorrect");
  112. question4_score.setTextColor(Color.BLACK);
  113. }
  114.  
  115. if (question5_answer_value == 1){
  116. question5_score.setText("Correct");
  117. } else {
  118. question5_score.setText("Incorrect");
  119. question5_score.setTextColor(Color.BLACK);
  120. }
  121.  
  122. private QuestionAndAnswers questionAndAnswers;
  123.  
  124. private TextView questionTextView;
  125.  
  126. private RadioGroup buttonsGroup;
  127.  
  128. private ArrayList<String> offeredAnswers;
  129.  
  130. private int i = 0;
  131.  
  132. public Question1() {
  133. // Required empty public constructor
  134. }
  135.  
  136. @Override
  137. public void onResume() {
  138. super.onResume();
  139. setRetainInstance(true);
  140. }
  141.  
  142.  
  143.  
  144. @Override
  145. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  146. Bundle savedInstanceState) {
  147. // Inflate the layout for this fragment
  148. View rootView = inflater.inflate(R.layout.fragment_question1, container, false);
  149.  
  150. // CREATING A LIST OF STRINGS (OFFERED ANSWERS)
  151. offeredAnswers = new ArrayList<String>();
  152. // ADDING THE STRINGS (ANSWERS)
  153. offeredAnswers.add("Ball sport");
  154. offeredAnswers.add("Combat sport");
  155. offeredAnswers.add("Team sport");
  156.  
  157. // CREATING A CUSTOM OBJECT
  158. questionAndAnswers = new QuestionAndAnswers("1. Wrestling is a :", offeredAnswers);
  159.  
  160. // FINDING THE TEXT VIEW TO DISPLAY THE ASSIGNED QUESTION
  161. questionTextView = (TextView) rootView.findViewById(R.id.question_text_view);
  162. questionTextView.setText(questionAndAnswers.getMyQuestion());
  163.  
  164. // FINDING THE RADIO GROUP TO DISPLAY THE BUTTONS FOR CHOOSING THE ANSWER
  165. buttonsGroup = (RadioGroup) rootView.findViewById(R.id.radio_group);
  166.  
  167. /* USING AN ARRAY LIST OF ANSWERS TO DISPLAY ALONGSIDE CREATED BUTTONS ADDED TO THE RADIO GROUP */
  168. ArrayList possibilities = (questionAndAnswers.getMyAnswers());
  169.  
  170. for (int a = 0; a < possibilities.size(); a++){
  171. RadioButton buttons = new RadioButton(getContext());
  172. buttons.setText(possibilities.get(a).toString());
  173. buttons.setTextColor(Color.parseColor("#303F9F"));
  174. buttons.setTextSize(18);
  175. buttons.setPadding(20, 30, 20, 30);
  176. buttonsGroup.addView(buttons, a);
  177. }
  178.  
  179. // USING PREFERENCE MANAGER TO WRITE TO THE FILE
  180. final SharedPreferences my_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
  181.  
  182. // FINDING THE VIEW PAGER FROM THE QUIZ APP ACTIVITY
  183. final ViewPager viewPager1 = (ViewPager) QuizApp.mInstance.childPager.findViewById(R.id.pager_quiz_app);
  184.  
  185. // GETTING THE POSTITION OF THE NEST FRAGMENT (QUESTION5)
  186. final int nextFragment1 = viewPager1.getCurrentItem() + 1;
  187.  
  188.  
  189. // STORING THE VALUE IN CASE THE PLAYER HAVEN'T ANSWERED ANY QUESTIONS
  190. SharedPreferences.Editor lostValue = my_preferences.edit();
  191. lostValue.putInt("value1", 0);
  192. lostValue.apply();
  193.  
  194.  
  195. int count1 = buttonsGroup.getChildCount();
  196. for (int i = 0; i < count1; i++){
  197. buttonsGroup.getChildAt(i).setOnClickListener(new View.OnClickListener() {
  198. @Override
  199. public void onClick(View v) {
  200.  
  201. final SharedPreferences.Editor sharedPrefEditor2 = my_preferences.edit();
  202. if (buttonsGroup.getChildAt(1).isPressed()) {
  203. sharedPrefEditor2.putInt("value1", 1);
  204. Toast toast = Toast.makeText(getContext(), "Mamu ti 100 puta", Toast.LENGTH_SHORT);
  205. toast.show();
  206. }
  207. sharedPrefEditor2.apply();
  208.  
  209. // SETTING THE VIEW PAGER TO SWITCH TO NEXT FRAGMENT UPON CLICKING THE BUTTON
  210. viewPager1.setCurrentItem(nextFragment1);
  211. }
  212. });
  213.  
  214. }
  215.  
  216. return rootView;
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement