【发布时间】:2014-08-08 16:27:50
【问题描述】:
我正在创建一个包含不同主题的 Android 应用测验。所以我在我的数据库中为 Quiz1、Quiz2、Quiz3 等提供了主题和不同表格的按钮......
这是我的 Quiz.java 代码
public class Quiz extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
Button quiz1 = (Button) findViewById(R.id.quiz1Btn);
quiz1.setOnClickListener(this);
Button quiz2 = (Button) findViewById(R.id.quiz2Btn);
quiz2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.quiz1Btn :
//Get Question set
List<Question> questions = getQuestionSetFromDb();
//Initialize quiz with retrieved question set ///
CurrentQuiz c = new CurrentQuiz();
c.setQuestions(questions);
c.setNumRounds(getNumQuestions());
((MLearningApp)getApplication()).setCurrentGame(c);
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 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(numQuestions);
myDbHelper.close();
return questions;
}
// Method to return the number of questions for the game
private int getNumQuestions() {
int numRounds = 10;
return numRounds;
}
这是我在 DBHelper.java 中的查询
public List<Question> getQuestionSet(int numQ){
List<Question> questionSet = new ArrayList<Question>();
Cursor c = myDataBase.rawQuery("SELECT * FROM Quiz1" + " ORDER BY RANDOM() LIMIT " + numQ, null);
while (c.moveToNext()){
//Log.d("QUESTION", "Question Found in DB: " + c.getString(1));
Question q = new Question();
q.setQuestion(c.getString(1));
q.setAnswer(c.getString(2));
q.setOption1(c.getString(3));
q.setOption2(c.getString(4));
questionSet.add(q);
}
return questionSet;
}
我的问题是,如何在我的数据库中为 Quiz2 按钮选择 Quiz2 TABLE;并且与 Quiz3 按钮等数据库中的 Quiz3 TABLE 相同。
到目前为止,我的 android 应用程序仅适用于 Quiz1,因为它只从我的数据库中检索一个用于 Quiz1 的表。
【问题讨论】:
-
为什么每个测验都使用单独的表格?您应该有一个带有测验编号列的表格。
-
注明。我将在我的桌子上添加问题编号,而不是使用不同的桌子。但如果是这种情况,我应该如何处理我的 SELECT QUERY 语句?
标签: java android sqlite mobile