I have a RadioGroup in which I have 5 RadioButton's. Also I have a button next. On click of next button the text of radiobuttons changes. It all works fine but some of the radio buttons are not selected. This is my xml file
public class SampleTestQuestionsActivity extends AppCompatActivity {
String totalques, timee, namee, idd;
String strServerResponse;
ProgressDialog nDialog;
ConnectionDetector cd;
Pojo pojo;
SamplePaperPojo samplePaperPojo;
RadioButton s_rb_1, s_rb_2, s_rb_3, s_rb_4, s_rb_5;
RadioGroup s_rbgrp;
private Toolbar toolbar;
TextView s_section, s_time, s_question;
Button s_submit, s_next, s_previous;
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
long startTime;
private final long interval = 1 * 1000;
final Context context = this;
ArrayList<String> al_que_title;
ArrayList<String> al_que_id;
ArrayList<String> al_ans1;
ArrayList<String> al_ans2;
ArrayList<String> al_ans3;
ArrayList<String> al_ans4;
ArrayList<String> al_ans5;
ArrayList<String> al_correct;
ArrayList<String> al_exp;
ArrayList<String> al_desc;
String submitQuestionId;
ArrayList<SamplePaperPojo> sampleTest;
RadioButton selectedRbButton;
public static int inc = 0;
String correctAns;
ArrayList<String> selectedAns;
int selectedpos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_test_questions);
toolbar = (Toolbar) findViewById(R.id.app_bar);
toolbar.setTitle("Sample Tests");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
al_que_title = new ArrayList<String>();
al_que_id = new ArrayList<String>();
al_ans1 = new ArrayList<String>();
al_ans2 = new ArrayList<String>();
al_ans3 = new ArrayList<String>();
al_ans4 = new ArrayList<String>();
al_ans5 = new ArrayList<String>();
al_correct = new ArrayList<String>();
al_exp = new ArrayList<String>();
al_desc = new ArrayList<String>();
selectedAns = new ArrayList<String>();
Intent i =getIntent();
namee = i.getStringExtra("test_name");
totalques = i.getStringExtra("test_ques");
timee = i.getStringExtra("test_time");
idd = i.getStringExtra("test_id");
sampleTest = new ArrayList<SamplePaperPojo>();
Log.e("test_id", ""+idd);
Long ti = Long.valueOf(timee);
startTime = 1000*ti;
s_section = (TextView) findViewById(R.id.sampleSectionName);
s_time = (TextView) findViewById(R.id.sampleTimer);
s_question = (TextView) findViewById(R.id.sampleQuestion);
s_submit = (Button) findViewById(R.id.sampleSubmitAnswer);
s_next = (Button) findViewById(R.id.sampleNext);
s_previous = (Button) findViewById(R.id.samplePrevious);
s_rbgrp = (RadioGroup) findViewById(R.id.s_rbgrp);
s_rb_1 = (RadioButton) findViewById(R.id.SA);
s_rb_2 = (RadioButton) findViewById(R.id.SB);
s_rb_3 = (RadioButton) findViewById(R.id.SC);
s_rb_4 = (RadioButton) findViewById(R.id.SD);
s_rb_5 = (RadioButton) findViewById(R.id.SE);
countDownTimer = new MyCountDownTimer(startTime, interval);
s_time.setText(s_time.getText() + String.valueOf(startTime / 1000));
new NetCheck().execute();
s_next.setVisibility(View.GONE);
s_previous.setVisibility(View.GONE);
s_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int index_selected = s_rbgrp.indexOfChild(s_rbgrp
.findViewById(s_rbgrp.getCheckedRadioButtonId()));
// get selected radio button from radioGroup
int selectedId = s_rbgrp.getCheckedRadioButtonId();
if (selectedId==-1){
AlertDialog alertDialog = new AlertDialog.Builder(
SampleTestQuestionsActivity.this).create();
alertDialog.setMessage("Please select atleast one answer.");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
s_next.setVisibility(View.VISIBLE);
}
});
s_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
submitQuestionId = al_que_id.get(inc).toString();
s_rb_1.setTextColor(Color.parseColor("#000000"));
s_rb_2.setTextColor(Color.parseColor("#000000"));
s_rb_3.setTextColor(Color.parseColor("#000000"));
s_rb_4.setTextColor(Color.parseColor("#000000"));
s_rb_5.setTextColor(Color.parseColor("#000000"));
int selectedId = s_rbgrp.getCheckedRadioButtonId();
selectedRbButton = (RadioButton) findViewById(selectedId);
selectedRbButton.setChecked(false);
inc = inc + 1;
s_question.setText("" + al_que_title.get(inc).toString());
s_rb_1.setText("" + al_ans1.get(inc).toString());
s_rb_2.setText("" + al_ans2.get(inc).toString());
s_rb_3.setText("" + al_ans3.get(inc).toString());
s_rb_4.setText("" + al_ans4.get(inc).toString());
s_rb_5.setText("" + al_ans5.get(inc).toString());
s_next.setVisibility(View.GONE);
}
});
}
private class NetCheck extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
nDialog = new ProgressDialog(SampleTestQuestionsActivity.this);
nDialog.setMessage("Loading..");
nDialog.setTitle("Please Wait");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.e("Post exec calleld", "dfds");
nDialog.dismiss();
s_question.setText("" + al_que_title.get(inc).toString());
s_rb_1.setText("" + al_ans1.get(inc).toString());
s_rb_2.setText("" + al_ans2.get(inc).toString());
s_rb_3.setText("" + al_ans3.get(inc).toString());
s_rb_4.setText("" + al_ans4.get(inc).toString());
s_rb_5.setText("" + al_ans5.get(inc).toString());
countDownTimer.start();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
cd = new ConnectionDetector(getApplicationContext());
if (!cd.isConnectingToInternet()) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(
new Runnable() {
@Override
public void run() {
AlertDialog alertDialog = new AlertDialog.Builder(
SampleTestQuestionsActivity.this).create();
alertDialog.setMessage("Error connecting to internet.");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
}
);
}
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpRequest = new HttpPost(
"http://url");
httpRequest.setHeader("Content-Type", "application/json");
SharedPreferences preff = getSharedPreferences(
"MyPref", MODE_PRIVATE);
String userid = preff.getString("id", null);
Log.e("Student id", "" + userid);
JSONObject json = new JSONObject();
json.put("mocktest_id", idd);
json.put("section_id", 1);
Log.e("JSON Object", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
httpRequest.setEntity(se);
HttpResponse httpRes = httpClient.execute(httpRequest);
java.io.InputStream inputStream = httpRes.getEntity()
.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
strServerResponse = sb.toString();
Log.e("Server Response", "" + strServerResponse.toString());
if (strServerResponse != null) {
try {
JSONArray arr1 = new JSONArray(strServerResponse);
JSONObject jsonObj1 = arr1.getJSONObject(0);
samplePaperPojo = new SamplePaperPojo();
for (int i = 0; i < arr1.length(); i++) {
JSONObject jobjj11 = arr1
.getJSONObject(i);
String qq_id = jobjj11.optString("id");
String qq_title = jobjj11.optString("title");
String qq_des = jobjj11.optString("description");
String ans_a = jobjj11.optString("ans_a");
String ans_b = jobjj11.optString("ans_b");
String ans_c = jobjj11.optString("ans_c");
String ans_d = jobjj11.optString("ans_d");
String ans_e = jobjj11.optString("ans_e");
String right_ans = jobjj11.optString("right_ans");
String explanation = jobjj11.optString("explanation");
samplePaperPojo.setSampleQuesId(qq_id);
samplePaperPojo.setSampleQuesTitle(qq_title);
samplePaperPojo.setSampleAns1(ans_a);
samplePaperPojo.setSampleAns2(ans_b);
samplePaperPojo.setSampleAns3(ans_c);
samplePaperPojo.setSampleAns4(ans_d);
samplePaperPojo.setSampleAns5(ans_e);
samplePaperPojo.setSampleRightAns(right_ans);
samplePaperPojo.setSampleQuesDescription(qq_des);
samplePaperPojo.setSampleExplaination(explanation);
sampleTest.add(samplePaperPojo);
al_que_id.add(qq_id);
al_que_title.add(qq_title);
al_ans1.add(ans_a);
al_ans2.add(ans_b);
al_ans3.add(ans_c);
al_ans4.add(ans_d);
al_ans5.add(ans_e);
al_correct.add(right_ans);
al_exp.add(explanation);
al_desc.add(qq_des);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler",
"Couldn't get any data from the url");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
}
The radio button which I previously selected is not selected when the text is changed after i click on next button. Please help