0
我感到困惑。我正在開發一個測驗。我一直在試圖隨機化問題。我試圖在Cursor和Rawquery中隨機化,但無濟於事。Android應用程序測驗中的隨機和循環問題
同時,我希望應用程序在最後一個問題後循環回答問題。
這裏是我的Question.java(這是我的活動)
public class Question extends Activity {
public static final String TAG = Question.class.getSimpleName();
private TextView mTimer;
private Button mTimerButton;
private CountDownTimer mCountDownTimer;
private TextView mQuestionTextView;
private Button mNextQuestion;
private Button mHowtoplay2;
List<QuestionFaci> quesList;
int qid = 0;
QuestionFaci currentQ;
@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question);
mHowtoplay2 = (Button) findViewById(R.id.howtoplaybutton);
mHowtoplay2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startHowtoplay();
}
});
QuizHelper db = new QuizHelper(this);
quesList = db.getAllQuestions();
currentQ = quesList.get(qid);
mQuestionTextView = (TextView) findViewById(R.id.questionView );
mNextQuestion = (Button)findViewById(R.id.nextbutton);
View.OnClickListener listener = new View.OnClickListener(){
@Override
public void onClick(View v) {
currentQ=quesList.get(qid);
setQuestionView();
}
};
mNextQuestion.setOnClickListener(listener);
Log.d(TAG, "We're logging from the onCreate() method");
private void startHowtoplay(){
Intent intent = new Intent(this, Howtoplay.class);
startActivity(intent);
}
private void setQuestionView(){
mQuestionTextView.setText(currentQ.getQUESTION());
qid++;
}
}
這裏是我的QuizHelper
public class QuizHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "creativequestion";
private static final String TABLE_QUEST = "quest";
private static final String KEY_ID = "qid";
private static final String KEY_QUEST = "question";
private SQLiteDatabase dbase;
public QuizHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
dbase = db;
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " (" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUEST + " TEXT)";
db.execSQL(sql);
addQuestion();
}
private void addQuestion() {
QuestionFaci q1 = new QuestionFaci("OTHER USES: Name other uses of a Hammer. \n\n Example: Stir a soup.");
this.addQuestion(q1);
QuestionFaci q2 = new QuestionFaci("RHYMES: Words that rhymes with Rice. \n\n Example: Ice");
this.addQuestion(q2);
QuestionFaci q3 = new QuestionFaci("WITH: I can cook eggs with... \n\n Example: A Piece of Plywood");
this.addQuestion(q3);
QuestionFaci q4 = new QuestionFaci("WITHOUT: I can wash my clothes without... \n\n Example: My Aunt");
this.addQuestion(q4);
QuestionFaci q5 = new QuestionFaci("I WILL: If I was Bill Gates, I will... \n\n Example: Buy a spaceship");
this.addQuestion(q5);
QuestionFaci q6 = new QuestionFaci("CREATE A MOVIE TITLE: A NIGHT \n\n Example: To Remember");
this.addQuestion(q6);
QuestionFaci q7 = new QuestionFaci("OTHER NAMES: Other names of a cow \n\n Example: Milk giver");
this.addQuestion(q7);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_QUEST);
onCreate(db);
}
private void addQuestion(QuestionFaci quest) {
ContentValues values = new ContentValues();
values.put(KEY_QUEST, quest.getQUESTION());
dbase.insert(TABLE_QUEST, null, values);
}
public List<QuestionFaci> getAllQuestions(){
List<QuestionFaci> quesList = new ArrayList<QuestionFaci>();
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
dbase = this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
if (cursor.moveToFirst()){
do{
QuestionFaci quest = new QuestionFaci();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quesList.add(quest);
} while (cursor.moveToNext());
}
return quesList;
}
}
這是我QuestionFaci在我的getter和setter
public class QuestionFaci extends Activity {
private int ID;
private String QUESTION;
public QuestionFaci(){
ID=0;
QUESTION= "";
}
public QuestionFaci (String qUESTION){
QUESTION = qUESTION;
}
public int getID(){
return ID;
}
public String getQUESTION(){
return QUESTION;
}
public void setID(int id){
ID = id;
}
public void setQUESTION(String qUESTION){
QUESTION = qUESTION;
}
}
你知道在我的測驗顯示數據庫中的最後一個問題後,我可以怎樣循環回去嗎? –
數據庫的最後一個問題或將顯示的最後一個問題?目前上面的答案只是設置隨機問題,它仍然可以顯示相同的問題。如果你想讓每一個問題都不會再出現,那麼你將不得不使用一個布爾值,在顯示每個問題後將其設置爲true。 「迴環」是什麼意思? –
我想出了託尼。我放了一個if和else語句。我想循環問題,而不管方法是否達到數據庫中最後一個問題。之前,它指出,應用程序崩潰,只要它顯示我的數據庫中的最後一個問題。 –