Note that there are some explanatory texts on larger screens.

plurals
  1. POsetOnCheckedChangeListener android
    text
    copied!<p>I am developing a quiz application. It is such multiple choice questions. The system retrieves questions from re-built sqlite database. The database has 6 questions, but when the last question is answered, i get error. Also, sometimes, radiobutton (answer) has to be checked 2 times, cant recognize the first checked. </p> <p>It is the code </p> <pre><code>public class LatihanActivity extends Activity{ private RadioButton radioButton; private TextView quizQuestion; private TextView tvScore; private TextView answerDesc; AlmagAdapter adapter=null; AlmagHelper helper=null; private int rowIndex = 1; private static int score=0; private static String description; private int questNo=0; private boolean checked=false; private boolean flag=true; private RadioGroup radioGroup; String[] corrAns = new String[6]; final DatabaseHelper db = new DatabaseHelper(this); Cursor c1; Cursor c2; Cursor c3; int counter=1; String label; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String options[] = new String[19]; helper=new AlmagHelper(this); final RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1); LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT); try { db.createDataBase(); } catch (IOException e) { e.printStackTrace(); } c3 = db.getCorrAns(); tvScore = (TextView) findViewById(R.id.tvScore); answerDesc = (TextView) findViewById(R.id.answerDesc); for (int i=0;i&lt;4;i++) { corrAns[i]=c3.getString(0); c3.moveToNext(); } radioGroup = (RadioGroup) findViewById(R.id.rdbGp1); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub for(int i=0; i&lt;group.getChildCount() ; i++) { RadioButton btn = (RadioButton) group.getChildAt(i); if (btn.isPressed() &amp;&amp; btn.isChecked() &amp;&amp; questNo &lt; 6) { if (corrAns[questNo].equals(btn.getText()) &amp;&amp; flag==true) { score++; flag=false; checked = true; } else if(checked==true) { score--; flag=true; checked = false; } Log.e("corrAns[questNo]",corrAns[questNo]); return; } } } }); quizQuestion = (TextView) findViewById(R.id.TextView01); displayQuestion(); Button btnNext = (Button) findViewById(R.id.btnNext); btnNext.setOnClickListener(btnNext_Listener); Button btnSave = (Button) findViewById(R.id.btnSave); btnSave.setOnClickListener(btnSave_Listener); } private View.OnClickListener btnNext_Listener= new View.OnClickListener() { @Override public void onClick(View v) { flag=true; checked = false; questNo++; if (questNo &lt; 6) { c1.moveToNext(); displayQuestion(); } } }; /*Called when save button is clicked*/ private View.OnClickListener btnSave_Listener= new View.OnClickListener() { @Override public void onClick(View v) { tvScore.setText("Score: " + Integer.toString(score) + "/6"); Log.e("Score:", Integer.toString(score)); } }; private void displayQuestion() { //Fetching data quiz data and incrementing on each click c1=db.getQuiz_Content(rowIndex); c2 =db.getAns(rowIndex++); quizQuestion.setText(c1.getString(0)); radioGroup.removeAllViews(); for (int i=0;i&lt;4;i++) { radioButton = new RadioButton(this); radioButton.setText(c2.getString(0)); radioButton.setId(i); c2.moveToNext(); radioGroup.addView(radioButton); } } } </code></pre> <p><strong>These are the errors</strong></p> <pre><code>ERROR/corrAns[questNo](299): am ERROR/Score:(299): 1 ERROR/corrAns[questNo](299): is ERROR/corrAns[questNo](299): is ERROR/Score:(299): 2 ERROR/corrAns[questNo](299): were ERROR/corrAns[questNo](299): were ERROR/Score:(299): 3 ERROR/corrAns[questNo](299): under ERROR/corrAns[questNo](299): under ERROR/Score:(299): 4 </code></pre> <p>Thanks </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload