Android问答随机问题

Android问答随机问题,android,Android,如何将使用RadioButton显示的问题随机化。我查看了其他帖子,但答案对我来说并不适用 import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.media.MediaPlayer; import android.os.Bundle; import android.util.Log; import android.view

如何将使用
RadioButton
显示的问题随机化。我查看了其他帖子,但答案对我来说并不适用

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class QuizActivity extends Activity {
    int score = 0;
    int qnum = 1;
    TextView txtQuestion;
    RadioButton rda, rdb, rdc, rdd;
    Button butNext;
    RadioGroup rdgrp;

    String corAnswer = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.activity_quiz);
        DbHelper db = new DbHelper(this);
        rdgrp = (RadioGroup) findViewById(R.id.questionAndAnswers);
        txtQuestion = (TextView) findViewById(R.id.textView1);
        rda = (RadioButton) findViewById(R.id.radio0);
        rdb = (RadioButton) findViewById(R.id.radio1);
        rdc = (RadioButton) findViewById(R.id.radio2);
        rdd = (RadioButton) findViewById(R.id.radio3);
        butNext = (Button) findViewById(R.id.button1);


        corAnswer = "";
        onCreateQuestion();
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_quiz, menu);
        return true;
    }


    public void onCreateQuestion() {
        String level = getIntent().getExtras().getString("level");
        DbHelper db = new DbHelper(this);
        db.getQuestByLevel(level, qnum);

        txtQuestion.setText(db.question);
        rda.setText(db.optionA);
        rdb.setText(db.optionB);
        rdc.setText(db.optionC);
        rdd.setText(db.optionD);
        corAnswer = db.answer;

        qnum++;

    }

    public void onClickNext(View view) {
        String level = getIntent().getExtras().getString("level");
        DbHelper db = new DbHelper(this);
        db.getQuestByLevel(level, qnum);

        RadioGroup grp = (RadioGroup) findViewById(R.id.questionAndAnswers);
        RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
        if (answer == null)
        {
            Toast.makeText(QuizActivity.this, "select an answer please", Toast.LENGTH_SHORT).show();


            return;
        }
        if (corAnswer!= null && corAnswer.equalsIgnoreCase((String) answer.getText()))
        {
            score++;
            Log.d("answer", "Your score" + score);
        }
        if (qnum <= 5) {


        } else {
            Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
            Bundle b = new Bundle();
            b.putInt("score", score);
            intent.putExtras(b);
            startActivity(intent);
            finish();
        }

        txtQuestion.setText(db.question);
        rda.setText(db.optionA);
        rdb.setText(db.optionB);
        rdc.setText(db.optionC);
        rdd.setText(db.optionD);
        corAnswer = db.answer;
        qnum++;
        rdgrp.clearCheck();
    }


}
导入android.app.Activity;
导入android.content.Intent;
导入android.content.pm.ActivityInfo;
导入android.media.MediaPlayer;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.view;
导入android.widget.Button;
导入android.widget.RadioButton;
导入android.widget.RadioGroup;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.util.List;
公共类quizaActivity扩展了活动{
智力得分=0;
int qnum=1;
文本视图TXT问题;
单选按钮rda、rdb、rdc、rdd;
按钮butNext;
放射组rdgrp;
字符串corAnswer=“”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
此.setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u横向);
setContentView(R.layout.activity\u测验);
DbHelper db=新的DbHelper(this);
rdgrp=(放射组)findViewById(R.id.问卷和回答);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
rdd=(RadioButton)findViewById(R.id.radio3);
但是下一步=(按钮)findViewById(R.id.button1);
corAnswer=“”;
onCreateQuestion();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.activity\u测验,菜单);
返回true;
}
public void onCreateQuestion(){
字符串级别=getIntent().getExtras().getString(“级别”);
DbHelper db=新的DbHelper(this);
db.getQuestByLevel(level,qnum);
txtQuestion.setText(db.question);
rda.setText(db.optionA);
rdb.setText(db.optionB);
rdc.setText(db.optionC);
rdd.setText(db.optionD);
corAnswer=db.answer;
qnum++;
}
公共void onClickNext(视图){
字符串级别=getIntent().getExtras().getString(“级别”);
DbHelper db=新的DbHelper(this);
db.getQuestByLevel(level,qnum);
放射组grp=(放射组)findViewById(R.id.问题和回答);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
如果(答案==null)
{
Toast.makeText(quizaActivity.this,“请选择答案”,Toast.LENGTH_SHORT.show();
回来
}
if(corAnswer!=null&&corAnswer.equalsIgnoreCase((字符串)answer.getText())
{
分数++;
Log.d(“回答”、“你的分数”+分数);
}

如果(qnum将您的问题放入
数组
中,则将此数组与以下内容混洗:

List<String> myArray = new ArrayList<String>(Arrays.asList(questionArray));
Collections.shuffle(myArray);
正如您在这里看到的,我有一个简单的表格[cyril_test],如下所示:

然后使用ORDER BY RANDOM()执行查询:

结果是:


你的具体问题是什么?你看了哪些帖子,哪些不起作用?这里没有问题。我只需要随机展示一下questions@Cyril如果没有问题,就没有问题question@BlakeYarbrough对不起,我没有说清楚。我的意思是在我发布的代码中没有错误。我的问题是如何显示问题我的问题在数据库中。有没有什么方法可以在不将它们放入数组的情况下对它们进行洗牌?对不起,我刚刚开始编写代码。这是一个很好的建议。我没有想到SQL查询+1@NicolasCortell你能看看我的问题吗?我不知道该在哪里下单random@Cyril我举一个例子:“按随机顺序从某个表中选择*限制1”。现在尝试使其适应您的查询。@NicolasCortell但这就是我的查询的外观字符串selectQuery=“SELECT*FROM”+TABLE\u quick+”,其中“+q\u level+”=”“+level+”、“+QuestionNumber+”=“+qnum+”;
... ORDER BY RANDOM()...