Android : Android: RadioGroup shows not correct child id

on Sunday, September 7, 2014


In my program when user clicks on row in ListView with spicified Test some RadioButtons from a file are programmatically created in the RadioGroup in xml layout. When user clicks "Confirm" button behind the RadioGroup, program shows a toast with getCheckedRadioButtonId. It works fine until user clicks back button that returns him to the main screen. If he clicks Test row again every getCheckedRadioButtonId is increased by RadioButtons count. Sorry for my english...


Here's code:



public void onTest (View view)
{
setContentView(R.layout.test);
try
{
String test = ((TextView) view).getText().toString();
TextView tvn = (TextView) findViewById(R.id.testName1);
TextView tvq = (TextView) findViewById(R.id.testQuestion1);
RadioGroup rga = (RadioGroup) findViewById(R.id.testAnswers1);
File tf = new File(getDir("Data", 0), test);
File ql = new File(tf, "ql.qlist");
BufferedReader brql = new BufferedReader(new FileReader(ql));
String quest = brql.readLine();
brql.close();
File al = new File(tf, quest + ".txt");
BufferedReader bral = new BufferedReader(new FileReader(al));
String ta;
ArrayList<String> aa = new ArrayList<String>();
while ((ta = bral.readLine()) != null)
aa.add(ta);
bral.close();
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tvn.setText(test);
tvq.setText(quest);
for (int i = 1; i < aa.size(); i++)
{
RadioButton rba = new RadioButton(this);
rba.setText(aa.get(i));
rga.addView(rba, i - 1, lp);
}
}
catch (IOException ioe)
{
System.out.println(ioe.getCause());
}
}

public void onAnswerConfirm (View view) throws IOException
{
RadioGroup rga = (RadioGroup) findViewById(R.id.testAnswers1);
TextView tvn = (TextView) findViewById(R.id.testName1);
TextView tvq = (TextView) findViewById(R.id.testQuestion1);
String test = tvn.getText().toString();
String quest = tvq.getText().toString();
File tf = new File(getDir("Data", 0), test);
File al = new File(tf, quest + ".txt");
BufferedReader bral = new BufferedReader(new FileReader(al));
int coa = Integer.parseInt(bral.readLine());
bral.close();
int cua = rga.getCheckedRadioButtonId();
String st = "no: ";
if (cua == coa)
st = "yes: ";
Toast.makeText(this, st + cua, Toast.LENGTH_SHORT).show();
}

0 comments:

Post a Comment