这只是this线程的延续。
我正在创建一个包含不同主题的Android应用测验。所以我有主题的按钮和quizNum column in my database的Quiz1,Quiz2,Quiz3等......
这是我的Quiz.java代码
public class Practice extends Activity implements OnClickListener{
//constants
public static final String QUIZNUM = "QUIZNUM";
public static final int Quiz1 = 1;
public static final int Quiz2 = 2;
public static final int Quiz3 = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.practice);
//Menu buttons
Button quiz1 = (Button) findViewById(R.id.q1Btn);
quiz1.setOnClickListener(this);
Button quiz2 = (Button) findViewById(R.id.q2Btn);
quiz2.setOnClickListener(this);
Button quiz3 = (Button) findViewById(R.id.q3Btn);
quiz3.setOnClickListener(this);
}
public void onClick(View v) {
Intent i;
List<Question> questions = getQuestionSetFromDb();
switch (v.getId()){
case R.id.q1Btn :
//Initialize quiz with retrieved question set
CurrentQuiz c = new CurrentQuiz();
c.setQuestions(questions);
c.setNumRounds(getNumQuestions());
c.setQuizNum(getQuizNum());
((MLearningApp)getApplication()).setCurrentGame(c);
i = new Intent(this, QuestionActivity.class);
startActivity(i);
break;
case R.id.q2Btn :
CurrentQuiz c2 = new CurrentQuiz();
c2.setQuestions(questions);
c2.setNumRounds(getNumQuestions());
c2.setQuizNum(getQuizNum());
((MLearningApp)getApplication()).setCurrentGame(c2);
i = new Intent(this, QuestionActivity.class);
startActivity(i);
break;
case R.id.q3Btn :
CurrentQuiz c3 = new CurrentQuiz();
c3.setQuestions(questions);
c3.setNumRounds(getNumQuestions());
c3.setQuizNum(getQuizNum());
((MLearningApp)getApplication()).setCurrentGame(c3);
i = new Intent(this, QuestionActivity.class);
startActivity(i);
break;
}
}
// Method that retrieves a random set of questions from db
private List<Question> getQuestionSetFromDb() throws Error {
int quizNum = getQuizNum();
int numQuestions = getNumQuestions();
DBHelper myDbHelper = new DBHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
List<Question> questions = myDbHelper.getQuestionSet(quizNum, numQuestions);
myDbHelper.close();
return questions;
}
private int getQuizNum() {
int qNum = Quiz1;
return qNum;
}
private int getNumQuestions() {
int numRounds = 10;
return numRounds;
}
这是我在DBHelper.java中的查询
public List<Question> getQuestionSet(int qNum, int numQ){
List<Question> questionSet = new ArrayList<Question>();
Cursor c = myDataBase.rawQuery("SELECT * FROM QUESTIONS WHERE qNum = " + qNum + " ORDER BY RANDOM() LIMIT " + numQ, null);
while (c.moveToNext()){
Question q = new Question();
q.setQuestion(c.getString(2));
q.setAnswer(c.getString(3));
q.setOption1(c.getString(4));
q.setOption2(c.getString(5));
questionSet.add(q);
}
return questionSet;
}
测验按钮现在正在运行,但它只从quiz1(db,1中的qNum表)中检索问题。我想在getQuizNum()方法中放入一个循环语句,但我不知道所需的代码
答案 0 :(得分:0)
测验按钮现在正在运行,但它只从quiz1
中检索问题
这是因为您使用getQuizNum()
方法来获取要查询的测验:
private List<Question> getQuestionSetFromDb() throws Error {
int quizNum = getQuizNum();
//rest of the code...
}
通过查看getQuizNum()
的实现,您总是返回1:
public static final int Quiz1 = 1;
//...
private int getQuizNum() {
int qNum = Quiz1;
return qNum;
}
更改getQuizNum
中的代码以返回其中一个可用结果。使用Random
值,例如:
public static final int MAX_QUIZ_QUANTITY = 3;
private int getQuizNum() {
Random r = new Random();
return r.nextInt(MAX_QUIZ_QUANTITY) + 1;
}
我误解了最初的问题,但问题仍然存在。您应该为测验按钮提供一个实现OnClickListener
界面的不同实例。另一种方法是将代码实现onClick
方法移动到内部类中,从而删除重复的代码并使其可以重用于不同的按钮:
//removed the implementation of OnClickListener from the class
//and moved into an inner class
//usually, your Activity doesn't implement OnClickListener unless there's a single button
public class Practice extends Activity {
//constants
public static final String QUIZNUM = "QUIZNUM";
public static final int Quiz1 = 1;
public static final int Quiz2 = 2;
public static final int Quiz3 = 3;
public static final int NUMBER_OF_QUESTIONS = 10;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.practice);
//Menu buttons
Button quiz1 = (Button) findViewById(R.id.q1Btn);
quiz1.setOnClickListener(new PracticeButton(Quiz1, NUMBER_OF_QUESTIONS));
Button quiz2 = (Button) findViewById(R.id.q2Btn);
quiz2.setOnClickListener(new PracticeButton(Quiz2, NUMBER_OF_QUESTIONS));
Button quiz3 = (Button) findViewById(R.id.q3Btn);
quiz3.setOnClickListener(new PracticeButton(Quiz3, NUMBER_OF_QUESTIONS));
}
//inner class that provides the common functionality for the buttons
class PracticeButton implements OnClickListener {
//declaring fields in the inner class to provide flexibility
//think of them as the necessary external parameters
//for the functionality of the button
int quizNumber;
int numQuestions;
//stating that the consumer of this class must provide the
//parameters for the functionality of the button
public PracticeButton(int quizNum, int numQuestions) {
this.quizNum = quizNum;
this.numQuestions = numQuestions;
}
//here you implement the onClick
public void onClick(View v) {
//the switch is not necessary anymore
Intent i;
List<Question> questions = getQuestionSetFromDb();
CurrentQuiz c = new CurrentQuiz();
c.setQuestions(questions);
c.setNumRounds(numQuestions);
c.setQuizNum(quizNum);
((MLearningApp)getApplication()).setCurrentGame(c);
i = new Intent(QuestionActivity.this, QuestionActivity.class);
startActivity(i);
}
//replaced Error by Exception
//Error should not be used, leave it to the JVM
private List<Question> getQuestionSetFromDb() throws Exception {
//moved these variables as fields
//int quizNum = this.quizNumber;
//int numQuestions = getNumQuestions();
DBHelper myDbHelper = new DBHelper(QuestionActivity.this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Exception("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
List<Question> questions = myDbHelper.getQuestionSet(quizNum, numQuestions);
myDbHelper.close();
return questions;
}
}
}
答案 1 :(得分:0)
让我展示一下代码的相关部分:
switch (v.getId()){
case R.id.q1Btn :
c.setQuizNum(getQuizNum());
break;
case R.id.q2Btn :
c2.setQuizNum(getQuizNum());
break;
case R.id.q3Btn :
c3.setQuizNum(getQuizNum());
break;
}
getQuizNum()
没用;您想要设置实际数字:
switch (v.getId()){
case R.id.q1Btn :
c.setQuizNum(1);
break;
case R.id.q2Btn :
c2.setQuizNum(2);
break;
case R.id.q3Btn :
c3.setQuizNum(3);
break;
}