简体   繁体   中英

Android Quiz App with SQLite database random choice

I am writing a simple multiple choice quiz app. The SQLite database table looks like this:

1 Question1 RightAnswer1
2 Question2 RightAnswer2
3 Question3 RightAnswer3 
and so on.

The activity layout looks like this.

Question TextView
Wrong Answer ListViewItem
Right Answer ListViewItem
Wrong Answer ListViewItem
Wrong Answer ListViewItem

I am going to choose Java random to select a random question from the table. My question is - how should I choose wrong answers from the same table so that they don't coincide with the right answer. And another question is how can I place the right answer at one of the four TextViews randomly? Should I use Java Collections.shuffle to shuffle those four positions?

I don´t write english very well, but take a look at my solution.

Create one listview of a call question class. In that class contains:
1 - One field string for the question.
2 - One field string for the answer.
3 - One field boolean for wrong or right.

I hope that helps.

Good luck!

You can build a class for your sqlite data.

public class name{
     private String question;
     private String answer;
     public name(String q,String a){
             question=q;
             question=a;
       }
     public String getQuestion{return question;}
     public String getAnswer {return answer;}
}

And after defining this class you can put your datas inside the ArrayList after retrieving from the Sqlite db.

ArrayList<your class name> list=new ArrayList<your class name>();
Cursor c=db.query(tablename,new String[]{your columns},null,null,null,null,null);
if(c.moveToFirst(){
    do{
       String question=c.getString(columnName);
       String answer=c.getString(ColumnBame);
       className cls=new className(question,answer);
       list.add(cls);
       }while(c.MoveToNext());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM