At this phase below code runs like when any user-
-press the radio button and press next button score increments on correct answer.
-press the radio button and press next button score decrements on incorrect answer.
-but when user press the back button my radio button get unchecked and if again i press the correct answer it increments the score.
I want that-
when either user press the back or next question button the previous answered questions should remain checked and does not increment the score again,
What I have tried:
public class QuizActivity extends ActionBarActivity {
private TextView quizQuestion;
private RadioGroup radioGroup;
private RadioButton optionOne;
private RadioButton optionTwo;
private RadioButton optionThree;
private RadioButton optionFour;
TextView text1;
private static final String FORMAT = "%02d:%02d:%02d";
int seconds, minutes;
private int currentQuizQuestion;
private int quizCount;
int score = 0;
int correct = 0;
int wrong = 0;
private QuizWrapper firstQuestion;
private List<QuizWrapper> parsedObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
quizQuestion = (TextView) findViewById(R.id.quiz_question);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
optionOne = (RadioButton) findViewById(R.id.radio0);
optionTwo = (RadioButton) findViewById(R.id.radio1);
optionThree = (RadioButton) findViewById(R.id.radio2);
optionFour = (RadioButton) findViewById(R.id.radio3);
Button previousButton = (Button) findViewById(R.id.previousquiz);
Button nextButton = (Button) findViewById(R.id.nextquiz);
text1 = (TextView) findViewById(R.id.t);
new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished) {
text1.setText("" + String.format(FORMAT,
TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
text1.setText("Time Over!");
}
}.start();
AsyncJsonObject asyncObject = new AsyncJsonObject();
asyncObject.execute("");
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int radioSelected = radioGroup.getCheckedRadioButtonId();
int userSelection = getSelectedAnswer(radioSelected);
int correctAnswerForQuestion = firstQuestion.getCorrectAnswer();
if ((radioGroup.getCheckedRadioButtonId() == -1)) {
score = correct - wrong;
Toast.makeText(getApplicationContext(), "Select an Answer Please" + score, Toast.LENGTH_LONG).show();
currentQuizQuestion++;
radioGroup.clearCheck();
if (currentQuizQuestion >= quizCount) {
Toast.makeText(QuizActivity.this, "End of the Quiz Questions", Toast.LENGTH_LONG).show();
return;
} else {
firstQuestion = parsedObject.get(currentQuizQuestion);
quizQuestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers = firstQuestion.getAnswers().split(",");
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
} else if (radioGroup.getCheckedRadioButtonId() != -1 && userSelection == correctAnswerForQuestion) {
correct++;
Toast.makeText(QuizActivity.this, "You got the answer correct" + "Correct-" + correct + "Wrong" + wrong, Toast.LENGTH_LONG).show();
currentQuizQuestion++;
radioGroup.clearCheck();
if (currentQuizQuestion >= quizCount) {
Toast.makeText(QuizActivity.this, "End of the Quiz Questions", Toast.LENGTH_LONG).show();
return;
} else {
firstQuestion = parsedObject.get(currentQuizQuestion);
quizQuestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers = firstQuestion.getAnswers().split(",");
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
} else if (radioGroup.getCheckedRadioButtonId() != -1) {
wrong++;
Toast.makeText(QuizActivity.this, "You got the answer correct" + "Correct-" + correct + "Wrong" + wrong, Toast.LENGTH_LONG).show();
currentQuizQuestion++;
radioGroup.clearCheck();
if (currentQuizQuestion >= quizCount) {
Toast.makeText(QuizActivity.this, "End of the Quiz Questions", Toast.LENGTH_LONG).show();
return;
} else {
firstQuestion = parsedObject.get(currentQuizQuestion);
quizQuestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers = firstQuestion.getAnswers().split(",");
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
} else {
Toast.makeText(QuizActivity.this, "UNKNOWN ERROR", Toast.LENGTH_LONG).show();
}
}
});
previousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentQuizQuestion--;
if (currentQuizQuestion < 0) {
return;
}
radioGroup.clearCheck();
firstQuestion = parsedObject.get(currentQuizQuestion);
quizQuestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers = firstQuestion.getAnswers().split(",");
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_quiz, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class AsyncJsonObject extends AsyncTask<String, Void, String> {
private ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httpPost = new HttpPost("https://xyz.php");
String jsonResult = "";
try {
HttpResponse response = httpClient.execute(httpPost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
System.out.println("Returned Json object " + jsonResult.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonResult;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(QuizActivity.this, "Downloading Quiz", "Wait....", true);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
System.out.println("Resulted Value: " + result);
parsedObject = returnParsedJsonObject(result);
if (parsedObject == null) {
return;
}
quizCount = parsedObject.size();
firstQuestion = parsedObject.get(0);
quizQuestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers = firstQuestion.getAnswers().split(",");
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = br.readLine()) != null) {
answer.append(rLine);
}
} catch (IOException e) {
e.printStackTrace();
}
return answer;
}
}
private List<QuizWrapper> returnParsedJsonObject(String result) {
List<QuizWrapper> jsonObject = new ArrayList<QuizWrapper>();
JSONObject resultObject = null;
JSONArray jsonArray = null;
QuizWrapper newItemObject = null;
try {
resultObject = new JSONObject(result);
System.out.println("Testing the water " + resultObject.toString());
jsonArray = resultObject.optJSONArray("quiz_questions");
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonChildNode = null;
try {
jsonChildNode = jsonArray.getJSONObject(i);
int id = jsonChildNode.getInt("id");
String question = jsonChildNode.getString("question");
String answerOptions = jsonChildNode.getString("possible_answers");
int correctAnswer = jsonChildNode.getInt("correct_answer");
newItemObject = new QuizWrapper(id, question, answerOptions, correctAnswer);
jsonObject.add(newItemObject);
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonObject;
}
private int getSelectedAnswer(int radioSelected) {
int answerSelected = 0;
if (radioSelected == R.id.radio0) {
answerSelected = 1;
}
if (radioSelected == R.id.radio1) {
answerSelected = 2;
}
if (radioSelected == R.id.radio2) {
answerSelected = 3;
}
if (radioSelected == R.id.radio3) {
answerSelected = 4;
}
return answerSelected;
}
private void uncheckedRadioButton() {
optionOne.setChecked(false);
optionTwo.setChecked(false);
optionThree.setChecked(false);
optionFour.setChecked(false);
}
}