无法获得Android测验的答案

时间:2014-08-07 21:47:31

标签: android sqlite

我有一个多项选择测验,从SQLite数据库类中提取问题和答案。只有在用户点击“下一步”按钮后,我才会显示答案。如何更改它以便在用户单击单选按钮的e后立即显示答案?

这里有代码 - 请不要问Logcat,因为没有错误:

public class QuizActivity extends ActionBarActivity {
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion, aTV;
RadioButton rda, rdb, rdc ,rdd;
Button butNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
    DbHelper db=new DbHelper(this);
    quesList=db.getAllQuestions();
    currentQ=quesList.get(qid);
    txtQuestion=(TextView)findViewById(R.id.textView1);
    rda=(RadioButton)findViewById(R.id.radio0);
    rdb=(RadioButton)findViewById(R.id.radio1);
    rdc=(RadioButton)findViewById(R.id.radio2);
    rdd=(RadioButton)findViewById(R.id.radio3);
    butNext=(Button)findViewById(R.id.button1);
    aTV=(TextView)findViewById(R.id.textAnswer);
    setQuestionView();
    butNext.setOnClickListener(new View.OnClickListener() {     
        @Override
        public void onClick(View v) {
            RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());

            Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
            if(currentQ.getANSWER().equals(answer.getText()))
            {
                score++;
                Log.d("score", "Your score"+score);
            }else {
                aTV.setText("Incorrect! The correct answer is " + currentQ.getANSWER());
            }
            if(qid<29){                 
                currentQ=quesList.get(qid);
                setQuestionView();
            }else{
                Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
                Bundle b = new Bundle();
                b.putInt("score", score); //Your score
                intent.putExtras(b); //Put your score to your next Intent
                startActivity(intent);
                finish();
            }
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.quiz, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
private void setQuestionView()
{
    txtQuestion.setText(currentQ.getQUESTION());
    rda.setText(currentQ.getOPTA());
    rdb.setText(currentQ.getOPTB());
    rdc.setText(currentQ.getOPTC());
    rdd.setText(currentQ.getOPTD());
    qid++;
}

}

2 个答案:

答案 0 :(得分:0)

为单选按钮实现CheckChangeListener。只要单选按钮检查状态发生变化,就会调用它。

cbSave.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int visibility = isChecked ? View.VISIBLE : View.GONE;
                findViewById(R.id.nameInstructions).setVisibility(visibility);
                findViewById(R.id.name).setVisibility(visibility);

}

答案 1 :(得分:0)

我找到了另一种使用Toast的方法,就像那样:

    if(currentQ.getANSWER().equals(answer.getText()))
            {
                score++;
                Log.d("score", "Your score"+score);
            }else {
                //uncomment next line if not using toast
                //aTV.setText("Incorrect! The correct answer is " + currentQ.getANSWER());
                //comment next 9 lines if not using toast
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.custom_toast_layout,(ViewGroup) findViewById(R.id.custom_toast_layout));
                TextView text = (TextView) layout.findViewById(R.id.textToShow);
                text.setText("Incorrect! The correct answer is " + currentQ.getANSWER());
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 30);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();
            }

创建了一个自定义Toast xml模板,以使吐司框显示在靠近页面中间的位置。虽然有趣的事情。为什么有LENGTH_SHORT和LENGTH_LONG但没有LENGTH_MEDIUM?

相关问题