setOnCheckedChangeListener android

时间:2011-10-18 14:32:47

标签: android sqlite radio-button listener checked

我正在开发一个测验应用程序。这是多选题。系统从重建的sqlite数据库中检索问题。数据库有6个问题,但是当最后一个问题得到解答时,我会收到错误。此外,有时,必须检查radiobutton(答案)2次,不能识别第一次检查。

这是代码

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<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<group.getChildCount() ; i++) {
                      RadioButton btn = (RadioButton) group.getChildAt(i);                    

                 if (btn.isPressed() && btn.isChecked() && questNo < 6)

                      {


                     if (corrAns[questNo].equals(btn.getText()) && 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 < 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<4;i++)
    {

        radioButton = new RadioButton(this);
        radioButton.setText(c2.getString(0));
        radioButton.setId(i);
        c2.moveToNext();
        radioGroup.addView(radioButton);
    }

}
}

这些是错误

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

谢谢

0 个答案:

没有答案