0

This is my first time to post anything here and i am not so good with the formatting yet and this post isnt perfect either. I just really need help. Im a noob.

The getannswer function is where the events are controlled depending on the users input.

public void getAnswer(String AnswerString) {
    if (this.ans.equals(AnswerString)) {

        // if conditions matches increase the int (score) by 1
        // and set the text of the score view
        this.score++;
        this.scored.setText("" + this.score);
        Toast.makeText(getApplicationContext(), "CORRECT!", Toast.LENGTH_SHORT).show();
        db.getQuestion();
    } else {

        // if the answer is wrong start activity and finish the test

        Intent intent = new Intent(Testactivity.this, result.class);
        Toast.makeText(getApplicationContext(), "WRONG, SORRY!", Toast.LENGTH_SHORT).show();
        db.getScore(test_entry.username, this.score);

        // passing the int value
        Bundle b = new Bundle();
        b.putInt("score", score); // Your score
        intent.putExtras(b); // Put your score to your next
        startActivity(intent);
        finish();
    }

}

getquestion is where I tried to implement a way to get questions from the database. My problem here is that I cant get it to skip questions it has already taken. As you can see, I tried to use images in some questions but that didnt work out so well and its not the point of the question.

public void getQuestion() {
    Random r = new Random();
    int n = r.nextInt(20);
    db = this.getReadableDatabase();
    ctr++;
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_QUEST + " WHERE `qid` = " + n + ";", null);
    if (cursor.moveToFirst()) {
        if(cursor.getInt(6) == 1) {
            ta.txtQuestion.setVisibility(View.VISIBLE);
            ta.txtQuestion.setText(cursor.getString(1).toString());
            /*Log.e("IMAGE", "I WAS HERE");
            ta.img.setVisibility(View.VISIBLE);
            int imageResource = draw.getResources().getIdentifier("@drawable/"+cursor.getString(1), null, PACKAGE);
            Drawable res = draw.getResources().getDrawable(imageResource);
            ta.img.setImageDrawable(res);
            //ta.img.setImageResource(imageResource);*/
        } else {
            ta.txtQuestion.setVisibility(View.VISIBLE);
            ta.txtQuestion.setText(cursor.getString(1).toString());
        }
        ta.button1.setText(cursor.getString(3).toString());
        ta.button2.setText(cursor.getString(4).toString());
        ta.button3.setText(cursor.getString(5).toString());
        ta.ans = cursor.getString(2).toString();
        Log.e("Current Question ID", Integer.toString(n));
        Testactivity.txtCounter.setText(Integer.toString(ctr));
    }

If I have left out any relevant piece of code or description.. Please let me know so I could update this post.

public List<Question> getAllQuestions() {
    List<Question> quesList = new ArrayList<Question>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
    dbase = this.getReadableDatabase();
    Cursor cursor = dbase.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
           // Question quest = new Question();
            //quest.setID(cursor.getInt(0));
            Question quest = new Question();
            quest.setID(cursor.getInt(0));
            quest.setQUESTION(cursor.getString(1));
            quest.setANSWER(cursor.getString(5));
            quest.setOPTA(cursor.getString(2));
            quest.setOPTB(cursor.getString(3));
            quest.setOPTC(cursor.getString(4));

            quesList.add(quest);
        } while (cursor.moveToNext());
    }
    // return quest list
    return quesList;
}

the one above is the newer function that gets the questions from the database. however. I am not able to use the NEWID() function which was supposed to return rows with randomly generated IDs. Or maybe my understanding is just wrong in this.

Kaze Notus
  • 37
  • 1
  • 8

1 Answers1

0

You should retrieve all of the questions, store them in whatever Collection you want, and then just go to the next question in getQuestion.

When you retrieve your questions from the database you can tell SQL to return in a random order.

If that SO answer for whatever reason has been changed here is the relevant part of the answer:

SELECT * FROM table
ORDER BY RANDOM()

So you'd be removing you WHERE clause from your query because you now want to retrieve all questions, however the ORDER BY NEWID() will return them in random order.

Community
  • 1
  • 1
CodyEngel
  • 1,501
  • 14
  • 22
  • This may sound like a dumb question.. What is the collection for? – Kaze Notus Nov 01 '16 at 21:46
  • Collections are used to store one or more items. I have a sneaking suspicion that I may be helping you with an assignment so I would encourage you to search for "Collection JavaDoc" and that will hopefully clear things up for you. – CodyEngel Nov 01 '16 at 23:21
  • So apparently.. I cant just call NEWID(). I thought that it was function. I also made what I hope to be a new function for getting questions. How do I use NEWID? – Kaze Notus Nov 03 '16 at 20:54
  • My apologies you want `ORDER BY RANDOM()` instead, `NEWID()` exists in MySQL but not SQLite. – CodyEngel Nov 03 '16 at 22:00