Java SQLite查询,为数据库中的特定测验选择问题

Java SQLite查询,为数据库中的特定测验选择问题,java,android,sqlite,mobile-application,Java,Android,Sqlite,Mobile Application,这只是线程的延续 我正在创建一个不同主题的android应用程序测试。所以我有主题按钮,还有Quiz1,Quiz2,Quiz3等等 下面是我的quick.java代码 public class Practice extends Activity implements OnClickListener{ //constants public static final String QUIZNUM = "QUIZNUM"; public static final int Quiz1 = 1; pub

这只是线程的延续

我正在创建一个不同主题的android应用程序测试。所以我有主题按钮,还有Quiz1,Quiz2,Quiz3等等

下面是我的quick.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;
}
测验按钮现在正在运行,但它只从数据库1中的quiz1 qNum表中检索问题。我想在getQuizNum方法中加入一个循环语句,但是我不知道需要什么代码

测验按钮现在正在运行,但它只检索来自quiz1的问题

这是因为您使用getQuizNum方法获取要查询的测验:

private List<Question> getQuestionSetFromDb() throws Error {
    int quizNum = getQuizNum();
    //rest of the code...
}
更改getQuizNum中的代码以返回一个可用结果。使用值,例如:

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;
        }
    }
}

让我展示代码的相关部分:

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;
}

尝试过这个,但它会在不同的测验中随机化测验问题。它不适合qNum 1=Quiz1按钮、qNum 2=Quiz2按钮、qNum 3=Quiz3按钮,当我单击Quiz1按钮时,有时会显示Quiz3中的问题。当我点击quiz2 btn时,会显示q1中的问题。是因为随机对吗?但是如何才能正确呢?@kaitlinries回答更新为另一种解决问题的方法。@kaitlinries对不起,我的错。代码已修复。它在QuestionActivity中给出了一个错误。这在intent和DBHelper中都表示在中无法访问任何类型的封闭实例scope@KaitlinReyes这很奇怪。我刚刚测试了代码并为我工作。我将如何从那里设置它?
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;
}