sqlite 데이터베이스로 여러 개의 퀴즈 응용 프로그램을 만들고 있습니다.sqlite 데이터베이스에서 행을 선택하면 같은 행이 표시되는 경우가 있습니다.
rawquery에서 "ORDER BY RANDOM()"을 사용하여 임의로 5 개의 행을 선택하지만 동일한 행이 때때로 행에 표시됩니다.
일부 경우 5 개의 다른 행을 표시합니다. 무작위로 선택하는 것도 올바르게 작동하므로 내 rawquery가 올바르게 작동하는 것 같습니다.
어느 경우에 동일한 질문과 선택 사항이 연속적으로 표시되는지 알 수 없습니다. 누군가 내 것과 같은 문제가 있습니까 ??
여기 내 코드입니다.
public class QuizActivity extends Activity implements OnClickListener {
int Correct = 0;
int Cnt = 1;
String fieldinfo;
String question;
String answer;
String choice1;
String choice2;
private MySQLHelper mDbHelper;
private SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
onQuestion();
}
public void onQuestion(){
mDbHelper = new MySQLHelper(this);
db = mDbHelper.getWritableDatabase();
try{
Cursor c = db.rawQuery("SELECT field, question, choice1, choice2, answer FROM WineQuiz ORDER BY RANDOM() LIMIT 5", null);
if(c.moveToFirst());
{
while(c.moveToNext()){
fieldinfo = c.getString(c.getColumnIndex("field"));
question = c.getString(c.getColumnIndex("question"));
choice1 = c.getString(c.getColumnIndex("choice1"));
choice2 = c.getString(c.getColumnIndex("choice2"));
answer = c.getString(c.getColumnIndex("answer"));
};
}
}catch(Exception e){
System.out.println("");;
}finally{
db.close();
}
ImageView image3 = (ImageView) findViewById(R.id.imageChoice1);
ImageView image4 = (ImageView) findViewById(R.id.imageChoice2);
TextView textChoice1 = (TextView) findViewById(R.id.textChoice1);
textChoice1.setText(choice1);
TextView textChoice2 = (TextView) findViewById(R.id.textChoice2);
textChoice2.setText(choice2);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
}
public void onClick(View v){
switch(v.getId()){
case R.id.imageChoice1:
if(answer.equals(choice1)){
Correct++;
Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Incorrect", Toast.LENGTH_SHORT).show();
}
break;
case R.id.imageChoice2:
if(choice2.equals(answer)){
Correct++;
Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Incorrect", Toast.LENGTH_SHORT).show();
}
break;
}
if(Cnt==5){
Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra("Correct", Correct);
startActivity(intent);
}else{
Cnt++;
onQuestion();
}
}
}
감사합니다. onQustion을 호출하면 매번 이미 선택된 행을 덮어 쓰는 것을 이해합니다. 나는 그것을 스스로 고치려고 노력하고있다. 그러나 당신은 나에게 약간의 예를 줄 수 있었느냐? 나는 프로그래밍에 대한 경험이 거의 없다. –