1

我是安卓新手。。

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

这是我的代码..

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

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

4

2 回答 2

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);
于 2013-09-09T12:39:11.037 回答
0

@shiv,你必须创建四个类来完成你的工作 QuizActivity 类 ResultActivity 类 Question 类 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++;
}
于 2014-12-19T07:07:49.097 回答