mercredi 27 janvier 2016

Making my questions of an IQ app random

hi i'm making an Intelligence Quotient application in Android... But i just can't make the questions random. When i retrieve all the question the app works. but when i put the order by clause the app stopped. Can someone please help me with the queries.

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "triviaQuiz";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA= "opta"; //option a
private static final String KEY_OPTB= "optb"; //option b
private static final String KEY_OPTC= "optc"; //option c
private SQLiteDatabase dbase;
public DbHelper(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_QUES
            + " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
            +KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT)";
    db.execSQL(sql);        
    addQuestions();
    //db.close();
}
private void addQuestions()
{
    Question q1=new Question(1,"Entomology is the science that studies","Behaviour of human beings", "Insects", "Birds", "Insects");
    this.addQuestion(q1);
    Question q2=new Question(2,"Which number should come next in the pattern? " +
            "37, 34, 31, 28", "25", "26", "27", "25");
    this.addQuestion(q2);
    Question q3=new Question(3,"When light bends as it enters a different medium the process is known as what?","Refraction", "Reflection","Diffraction","Refraction");
    this.addQuestion(q3);
    Question q4=new Question(4,"Earth is located in which galaxy?",  "Cigar Galaxy", "Pinwheel GAlaxy", "Milky Way","Milky Way");
    this.addQuestion(q4);
    Question q5=new Question(5,"Some months have 30 days, some have 31 days. How many months have 28 days?","1","2","12","12");
    this.addQuestion(q5);
    Question q6=new Question(6,"Divide 30 by half and add 10. What do you get?","15", "25", "70", "70");
    this.addQuestion(q6);
    Question q7=new Question(7,"If a doctor gives you 3 pills and tells you to take 1 pill every every half hour.\n"+
            " How long will it be before all pills had been taken?", "1hr30min", "1hr", "30min", "1hr");
    this.addQuestion(q7);
    Question q8=new Question(8,"If you have a match, and you enter a COLD and DARK room.\n"+
            " Which of the following will you light first?","Oil lamp", "Oil heater","Match","Match");
    this.addQuestion(q8);
    Question q9=new Question(9,"Which one of the five choices makes the best comparison?\n" +
            "PEACH is to HCAEP as 46251 is to:","51462", "15264", "25641","15264");
    this.addQuestion(q9);
    Question q10=new Question(10,"Mary, who is sixteen years old, is four times as old as her brother.\n"+
            "How old will Mary be when she is twice as old as her brother?","28","26","24","24");
    this.addQuestion(q10);
    Question q11=new Question(11,"If you rearrange the letters 'CIFAIPC' you would have the name of a(n):","Ocean","Animal","Country","Ocean");
    this.addQuestion(q11);
    Question q12=new Question(12,"John needs 13 bottles of water from the store. John can only carry 3 at a time.\n"+
            "What's the minimum number of trips John needs to make to the store?","3", "4", "5", "5");
    this.addQuestion(q12);
    Question q13=new Question(13,"If you rearrange the letters 'LNGEDNA' you have the name of a(n):", "River", "Country", "Animal", "Country");
    this.addQuestion(q13);
    Question q14=new Question(14,"What is the missing number in the sequence shown below?\n" +
            "1 - 8 - 27 - ? - 125 – 216","36", "45","64","64");
    this.addQuestion(q14);
    Question q15=new Question(15,"There is a big turtle. It is walking with a little turtle.\n"+
            " The little turtle is the big turtle's son but the big turtle isn't the dad. Who is the big turtle?","A stranger", "The mom", "The grandmother","The mom");
    this.addQuestion(q15);
    Question q16=new Question(16,"A guy is condemned to death. He has to choose a room.\n" +
            "room #1: A fiery inferno\n" +
            "room #2: 30 assassins with loaded guns.\n" +
            "room #3: A room full of hungry lions that didn't eat in 3 months.\n" +
            "Which room is the safest?","None","Room1","Room3","Room3");
    this.addQuestion(q16);
    Question q17=new Question(17,"What is Rupert the bear's middle name?", "He doesn't have one", "Middle","The", "The");
    this.addQuestion(q17);
    Question q18=new Question(18,"If there are three apples and you took two away, how many do you have?","1", "2","3","2");
    this.addQuestion(q18);
    Question q19=new Question(19,"Which is heavier, 100 pounds of rocks or 100 pounds of GOLD?",  "100 pounds of rocks", "100 pounds of GOLD", "They have the same weight","They have the same weight");
    this.addQuestion(q19);
    Question q20=new Question(20,"What has a tail but no body?","A gecko","A boa","A coin","A coin");
    this.addQuestion(q20);
    Question q21=new Question(21,"What is half of two plus two equal?", "2", "3","4", "3");
    this.addQuestion(q21);
    Question q22=new Question(22,"What is the beginning of eternity. The end of time and space.\n"+
            " The beginning of every end. And the end of every place?","The letter B", "The letter E","The letter T","The letter E");
    this.addQuestion(q22);
    Question q23=new Question(23,"What goes up and never comes down?", "Your age","Your smartness", "Your knowledge", "Your age");
    this.addQuestion(q23);
    Question q24=new Question(24,"If yesterday had been Wednesday's tomorrow and tomorrow is Sunday's yesterday, what day would today be?","Wednesday","Thursday","Friday","Friday");
    this.addQuestion(q24);
    Question q25=new Question(25,"What 5 letter word becomes shorter when you add 2 letters to it??","train","short","games","short");
    this.addQuestion(q25);
    Question q26=new Question(26,"Linda was born on December 25th , yet her birthday is always in summer. How is this possible?", "Linda lives in the Northern hemisphere ", "Linda lives in Florida","Linda lives in the Southern hemisphere", "Linda lives in the Southern hemisphere");
    this.addQuestion(q26);
    Question q27=new Question(27,"I am a four letter word. I am an animal. You use me to call your close ones. Who am I?","Dear", "Deer","Dire","Deer");
    this.addQuestion(q27);
    Question q28=new Question(28,"You need to spend a night in a building where there is no power.\n"+
            " There are three rooms in this building. On the door of each room there are three\n"+
            "different signs. One says 'Lions Inside - anyone goes inside becomes their supper', \n"
            +"the second one says 'Open the door and a machine gun starts firing'\n"+
            "and the third one says ‘Electric Room - touch anything and you will die’.\n"+
            "Which room would you choose?"
            , "The Electric Room","The room with Lions", "The room with machine gun", "The Electric Room");
    this.addQuestion(q28);
    Question q29=new Question(29,"If you were running a race and you passed the person in 2nd place.\n"+
            "What place would you be in now?","1st place","2nd place","3rd place","2nd place");
    this.addQuestion(q29);
    Question q30=new Question(30,"How many times can you subtract the number two from the number fifty?","Once", "25 times","26 times","Once");
    this.addQuestion(q30);
    Question q31=new Question(31,"I have four sides. Anyone can calculate my area if they come to know the length of any one side of mine.\n"+
            " I have four right angles within me. Which shape am I?", "Parallelogram","Rectangle", "Square", "Square");
    this.addQuestion(q31);
    Question q32=new Question(32,"In a lake, there is a patch of lily pads. Every day, the patch doubles in size.\n"+
            " If it takes 48 days for the patch to cover the entire lake, how long would it take for the patch to cover half the lake? ","24 days","30 days","47 days","47 days");
    this.addQuestion(q32);
    Question q33=new Question(33,"Chris father has 4 children; three are named Nana, Nene, and Nini. So what is is the 4th child's name?","Nono","Nunu","Chris","Chris");
    this.addQuestion(q33);
    Question q34=new Question(34,"Which number should come next in this series?\n"+
            "25,24,22,19,15,-", "12", "11","10", "10");
    this.addQuestion(q34);
    Question q35=new Question(35,"Which country gave the USA the 'Statute of Liberty?","Greece", "Germany","France","France");
    this.addQuestion(q35);
    Question q36=new Question(36,"Find the missing pattern\n"+
            "+ + + * * * * + + + + * * * + + + + ? ? *", "* *","+ *", "* +", "+ *");
    this.addQuestion(q36);
    Question q37=new Question(37,"Bodo weighs 120 pounds more than his sister Annie. Their combined weight is 140 pounds.\n"+
            "How many pounds does Bodo weigh?","120","130","150","130");
    this.addQuestion(q37);
    Question q38=new Question(38,"Johnny’s mother had three children. The first child was named April.\n"+
            " The second child was named May. What was the third child’s name?","June","July","Johnny","Johnny");
    this.addQuestion(q38);
    Question q39=new Question(39,"How many two cent stamps are there in a dozen?", "12 ", "24","48", "12");
    this.addQuestion(q39);
    Question q40=new Question(40,"How do mathematicians call a regular polygon with 11 side", "Hexadecagon", "Hendecagon","Heptadecagon", "Hendecagon");
    this.addQuestion(q40);
    Question q41=new Question(41,"How do you call an angle more than 90 degrees and less than 180 degrees?", "Obtuse","Acute", "Reflex", "Obtuse");
    this.addQuestion(q41);
    Question q42=new Question(42,"What's the only metal that's not a solid at room temperature?","Chromium","Nickel","Mercury","Mercury");
    this.addQuestion(q42);
    Question q43=new Question(43,"What is the chemical symbol of water?","HO","H2O","H3O","H2O");
    this.addQuestion(q43);
    Question q44=new Question(44,"What is the closest planet to the sun?", "Venus", "Mercury","Uranus", "Mercury");
    this.addQuestion(q44);
    Question q45=new Question(45,"How many legs does a spider have?", "6", "8","10", "8");
    this.addQuestion(q45);
    Question q46=new Question(46,"What comes next in the sequence?\n"+
            "8, 27, 64,__, 216, 343", "100","125", "200", "125");
    this.addQuestion(q46);
    Question q47=new Question(47,"On seeing in the mirror a clock is showing the time as 8:35. What is the actual time?","5:38","3:25","4:35","3:25");
    this.addQuestion(q47);

    Question q48=new Question(48,"Is it legal for a man to marry his widow’s sister?","Yes, in some states","Yes","No","No");
    this.addQuestion(q48);
    Question q49=new Question(49,"It was a sunny Sunday morning. Roger was reading, Bella was painting, little Johny was trying to walk,\n"+
            "the widow next door was the neighbor over the phone with her husband, Susie was in the kitchen,\n"+
            "and Tommy, the pet dog, was barking frantically. Then there was a loud bang, and everybody found Tommy dead in his kennel.\n"+
            "Who killed Tommy?", "Susie", "Bella and Roger","The neighbor", "The neighbor");
    this.addQuestion(q49);
    Question q50=new Question(50,"Which number should come next in the series?\n"+
            "1,2,6,24,120__?","150","360", "720", "720");
    this.addQuestion(q50);

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
    // Create tables again
    onCreate(db);
}
// Adding new question
public void addQuestion(Question quest) {
    //SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_ID, quest.getID());
    values.put(KEY_QUES, quest.getQUESTION()); 
    values.put(KEY_ANSWER, quest.getANSWER());
    values.put(KEY_OPTA, quest.getOPTA());
    values.put(KEY_OPTB, quest.getOPTB());
    values.put(KEY_OPTC, quest.getOPTC());
    // Inserting Row
    dbase.insert(TABLE_QUEST, null, values);        
}


public List<Question> getAllQuestions() {

    List<Question> quesList = new ArrayList<Question>();


    // Select All Query
    String selectQuery = "SELECT  * FROM" + TABLE_QUEST  + "ORDER BY RANDOM()";
    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));
            quest.setQUESTION(cursor.getString(1));
            quest.setANSWER(cursor.getString(2));
            quest.setOPTA(cursor.getString(3));
            quest.setOPTB(cursor.getString(4));
            quest.setOPTC(cursor.getString(5));
            quesList.add(quest);
        } while (cursor.moveToNext());
    }
    // return quest list
    return quesList;
}


public int rowcount()
{
    int row=0;
    String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    row=cursor.getCount();
    return row;
}

}

Aucun commentaire:

Enregistrer un commentaire