0
안드로이드 애플리케이션에서 루프 내에서 MCQ로 10 개의 질문을 표시해야합니다. sqlite datbase.So에서 질문을 얻고 있습니다. 결과를 SQL 쿼리로 제한하고 있습니다. 하지만 처음 버튼을 클릭하면 MCQ (사용자가 선택하는 4 개의 답변을 사용하고 있습니다) 질문이 성공적으로 표시됩니다. 두 번째로 버튼을 클릭하면 작동하지 않습니다. 그 문제는 무엇입니까? 다음은 내 코드입니다.루프에서 버튼 누름
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Button nextButton = (Button) findViewById(R.id.button1);
DataBaseHelper myDbHelper = new DataBaseHelper(MainActivity.this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to connect to database");
}
try {
myDbHelper.openDataBase();
Cursor c = myDbHelper.retriveQuestionsData();
setContentView(R.layout.quiz_home);
Log.d(Logcat, "Clicked");
nextButton.setText("Next");
String id = "";
String question = "";
int counter = 0;
int cursorsize = c.getCount();
Log.d(Logcat, "Cursor size is: " + cursorsize);
if (c.moveToFirst()) {
do {
counter = counter + 1;
Log.d(Logcat, "Counter value is: " + counter);
id = c.getString(0);
question = c.getString(1);
TextView tvw = (TextView) findViewById(R.id.textView1);
tvw.setText("Q" + counter + ":" + question + "?");
Cursor ansc = myDbHelper.retriveAnswersData(id);
if (ansc.moveToFirst()) {
do {
RadioGroup rgp = (RadioGroup) findViewById(R.id.radioGroup1);
rgp.setVisibility(View.VISIBLE);
for (int i = 0; i < rgp.getChildCount(); i++) {
int index = i + 1;
((RadioButton) rgp.getChildAt(i)).setText(ansc.getString(index));
}
String correctans = ansc.getString(5);
Log.d(Logcat, "database correct answer:" + correctans);
int selected = rgp.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) findViewById(selected);
String user_input = rb.getText().toString();
SQLiteDatabase tempdb = new TempDatabaseHelper(MainActivity.this).getWritableDatabase();
ContentValues cv = new ContentValues();
if (user_input.equals(correctans)) {
cv.put("input", "1");
tempdb.insert("user_input", null, cv);
Log.d(Logcat, "Correct");
} else {
cv.put("input", "0");
tempdb.insert("user_input", null, cv);
Log.d(Logcat, "Wrong");
}
Log.d(Logcat, "DATA INSERTED");
tempdb.close();
Log.d(Logcat, "DATABASE CLOSED");
Log.d(Logcat, "USER INPUT IS:" + user_input);
} while (ansc.moveToNext());
}
} while (c.moveToNext());
}
} catch (SQLException sqle) {
throw sqle;
}
myDbHelper.close();
//setContentView(R.layout.results);
}
});
방금 루프의 실행 시간 수를 확인하기 위해 카운터를 사용했습니다.
버튼 클릭 수신기에서 'setContentView (R.layout.quiz_home);' 뭐야 ... – EpicPandaForce
질문 페이지의 레이아웃 파일입니다. – CodeCanyon