0
我正在用sqlite數據庫進行多個測驗應用程序。從sqlite數據庫選擇行有時會顯示同一行
我在rawquery中使用「ORDER BY RANDOM()」隨機選擇5行,但同一行有時會連續出現。
它在某些情況下顯示5行不同。隨機選擇也正常工作,所以我的原始查詢似乎正常工作。
我只是無法找出在哪種情況下,它顯示了連續的相同的問題和選擇。 有沒有人有和我一樣的問題?
這是我的代碼。
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();
}
}
}
謝謝您的建議。我知道每次調用Qustion都會覆蓋已經選取的行。我試圖自己修復它,但你能給我舉個例子嗎?我幾乎沒有編程經驗。 – 2014-09-02 12:48:50