如何从sqlite数据库中随机选择答案

时间:2013-09-09 12:33:19

标签: android sqlite

我是android的新手..

我正在开发一个基于测验的应用程序。将有1个问题和4个选项(单选按钮)。我想随机从数据库中选择答案。我的数据库中有6列.1个是ID,第2个是问题,第3个是正确的,第4个和第5个。第六是错误的答案。那么,请告诉我怎么做?

这是我的代码..

public void abc()
{   
    score.setText("Score: "+ count);
    db=new DBAdapter(this);
    db.open();
    c=db.getText(id);
    String ques=c.getString(1);
    tv.setText(ques);
    String cans=c.getString(2);
    rb1.setText(cans);
    String wans1=c.getString(3);
    rb2.setText(wans1);
    String wans2=c.getString(4);
    rb3.setText(wans2);
    String wans3=c.getString(5);
    rb4.setText(wans3);

}

public void onClick(View v)
{
    try 
    {
        if(c.getCount()<1)     
        {

            Intent igameend=new Intent(this,Gameend.class);
            Bundle b=new Bundle();
            b.putString("Score",score.getText().toString());
            igameend.putExtras(b);
            startActivity(igameend);
            finish();
        }

        else if(rb1.isChecked())
        {
            count++;
        }

        rb1.setChecked(false);
        rb2.setChecked(false);
        rb3.setChecked(false);
        rb4.setChecked(false);
        abc();
        id++;

    }

    catch(Exception ex)
    {
Toast.makeText(getBaseContext(), ex.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

如果有任何错误..请告诉我.. 提前谢谢..

2 个答案:

答案 0 :(得分:0)

按顺序获取所有数据并将所有答案放入列表中,然后使用Collection.shuffle()对列表进行随机播放,如下所示:

List<Integer> list = new ArrayList<Integer>();

list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);

Collections.shuffle(list);

答案 1 :(得分:0)

@shiv,你必须创建四个类来完成你的工作 QuizActivity类 ResultActivity类 问题类 Dbhelper班

现在我要提一下QuizActivity类代码:

public class Knowledge_Test1 extends Activity {
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
@Override
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_testk1);
    DbHelper db=new DbHelper(this);
    quesList=db.getAllQuestions();
    currentQ=quesList.get(qid);
    txtQuestion=(TextView)findViewById(R.id.textView1);
    rda=(RadioButton)findViewById(R.id.radio0);
    rdb=(RadioButton)findViewById(R.id.radio1);
    rdc=(RadioButton)findViewById(R.id.radio2);
    butNext=(Button)findViewById(R.id.button1);
    setQuestionView();
    butNext.setOnClickListener(new View.OnClickListener() {     
        @Override
        public void onClick(View v) {
            RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
            Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
            if(currentQ.getANSWER().equals(answer.getText()))
            {
                score++;
                Log.d("score", "Your score"+score);
            }
            if(qid<5){                  
                currentQ=quesList.get(qid);
                setQuestionView();
            }else{
                Intent intent = new Intent(Knowledge_Test1.this, ResultActivity.class);
                Bundle b = new Bundle();
                b.putInt("score", score); //Your score
                intent.putExtras(b); //Put your score to your next Intent
                startActivity(intent);
                finish();
            }
        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_quiz, menu);
    return true;
}
private void setQuestionView()
{
    txtQuestion.setText(currentQ.getQUESTION());   
    rda.setText(currentQ.getOPTA());
    rdb.setText(currentQ.getOPTB());
    rdc.setText(currentQ.getOPTC());
    qid++;
}