簡體   English   中英

如何在測驗應用程序中為Firebase數據創建隨機數?

[英]How to create random number for Firebase data in quiz app?

我正在制作一個測驗應用程序,其中的問題和答案均來自Firebase。 我在Firebase中上傳了帶有選項的問題,現在想隨機檢索它們。 我努力了

Random number = new Random();
        number.nextInt(25);

問題是我不需要重復。 我只需要生成一個號碼,問題結束時,警報欄就會顯示出來,並向用戶評分活動。 這是我的主要活動

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (mScore != 0){
    mScore = savedInstanceState.getInt("QK");

    }
    else{
        mScore = 0;

    }
    setContentView(R.layout.activity_bioquiz);
    setAndroidContext(this);
    choice1b = findViewById(R.id.choice1);
    choice2b = findViewById(R.id.choice2);
    choice3b = findViewById(R.id.choice3);
    choice4b = findViewById(R.id.choice4);
    mScoreTextView = findViewById(R.id.Score);
    mQuestionTextView = findViewById(R.id.questionTextView);

        Random m = new Random();

    int mQuestionNo = m.nextInt(25);



    updateQuestion();
    View.OnClickListener A = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (choice1b.getText().equals(mAnswer)) {
                choice1b.setBackgroundColor(Color.GREEN);
                mScore = mScore + 1;
                mScoreTextView.setText("Score "+String.valueOf(mScore) + "/"+ " 100");
                Toast.makeText(Bioquiz.this, "You got it!", Toast.LENGTH_SHORT).show();
                updateQuestion();
            } else {
                updateQuestion();
                v.setBackgroundColor(Color.RED);
                Toast.makeText(Bioquiz.this, "oops Wrong Answer", Toast.LENGTH_SHORT).show();
            }

        }
    };
    View.OnClickListener B = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (choice2b.getText().equals(mAnswer)) {
                choice1b.setBackgroundColor(Color.GREEN);
                mScore = mScore + 1;
                mScoreTextView.setText("Score "+String.valueOf(mScore) + "/"+ " 100");
                Toast.makeText(Bioquiz.this, "You got it!", Toast.LENGTH_SHORT).show();
                updateQuestion();
            } else {
                updateQuestion();
                v.setBackgroundColor(Color.RED);
                Toast.makeText(Bioquiz.this, "oops Wrong Answer", Toast.LENGTH_SHORT).show();

            }

        }

    };
    View.OnClickListener C = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (choice4b.getText().equals(mAnswer)) {
                choice1b.setBackgroundColor(Color.GREEN);
                mScore = mScore + 1;
                mScoreTextView.setText("Score "+String.valueOf(mScore) + "/"+ " 100");
                Toast.makeText(Bioquiz.this, "You got it!", Toast.LENGTH_SHORT).show();
                updateQuestion();
            } else {
                v.setBackgroundColor(Color.RED);
                updateQuestion();
                Toast.makeText(Bioquiz.this, "oops Wrong Answer", Toast.LENGTH_SHORT).show();
            }

        }
    };

    View.OnClickListener D = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (choice4b.getText().equals(mAnswer)) {
                choice1b.setBackgroundColor(Color.GREEN);
                mScore = mScore + 1;
                mScoreTextView.setText("Score "+String.valueOf(mScore) + "/"+ " 100");
                Toast.makeText(Bioquiz.this, "You got it!", Toast.LENGTH_SHORT).show();
                updateQuestion();

            } else {

                v.setBackgroundColor(Color.RED);
                updateQuestion();
                Toast.makeText(Bioquiz.this, "oops Wrong Answer", Toast.LENGTH_SHORT).show();
            }

        }
    };
    choice1b.setOnClickListener(A);
    choice2b.setOnClickListener(B);
    choice3b.setOnClickListener(C);
    choice4b.setOnClickListener(D);

}

    public void updateQuestion() {
        Random number = new Random();
        number.nextInt(25);



    Firebase questionRef = new Firebase("https://mcatbiologymcqs.firebaseio.com/"+mQuestionNo+"/question");
    questionRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String question = dataSnapshot.getValue(String.class);
            mQuestionTextView.setText(question);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    Firebase choice1Ref = new Firebase("https://mcatbiologymcqs.firebaseio.com/" +mQuestionNo + "/choice1");
    choice1Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice1 = dataSnapshot.getValue(String.class);
            choice1b.setText(choice1);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mChoice2Ref = new Firebase("https://mcatbiologymcqs.firebaseio.com/"+mQuestionNo+"/choice2");
    mChoice2Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice2 = dataSnapshot.getValue(String.class);
            choice2b.setText(choice2);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mChoice3Ref = new Firebase("https://mcatbiologymcqs.firebaseio.com/"+mQuestionNo+"/choice3");
    mChoice3Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice3 = dataSnapshot.getValue(String.class);
            choice3b.setText(choice3);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    mChoice4Ref = new Firebase("https://mcatbiologymcqs.firebaseio.com/"+mQuestionNo+"/choice4");
    mChoice4Ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String choice4 = dataSnapshot.getValue(String.class);
            choice4b.setText(choice4);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    Firebase answerRef = new Firebase("https://mcatbiologymcqs.firebaseio.com/"+mQuestionNo+"/answer");
    answerRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            mAnswer = dataSnapshot.getValue(String.class);

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

    if (mScoreTextView == null){

        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Quiz Over");
        if (mScore ==20){alert.setMessage("Very Good");}
        else {
            alert.setMessage("You need to Work Hard");
        }
        alert.setPositiveButton("View Score", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
           Intent score = new Intent(Bioquiz.this,Score.class);

           startActivity(score);
            }
        });
        alert.setCancelable(false);
        alert.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
           finish();
            }
        });
        alert.show();


        }
    }

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent m = new Intent(Bioquiz.this,MainActivity.class);
    startActivity(m);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("SK",mScore);





}}

似乎您正在嘗試從25個可能的問題中隨機抽取一個問題,並且不想重復獲得同一問題。 您可以保留一個問題編號列表,並在用戶看到該問題后將其從列表中刪除。

//declare the list at the top with the other variables
public List<Integer> questionNos = new ArrayList<>();

在onCreate更改此行

int mQuestionNo = m.nextInt(25);

這些行

for (int i = 0; i < numberOfQuestions; i++) {
    questionNos.add(i);
}
Random r = new Random();
    int index = r.nextInt(questionNos.size()-1);
    int mQuestionNo = questionNos.remove(index);

在updateQuestion()中更改這些行

Random number = new Random();
number.nextInt(25);

對此

    Random r = new Random();
    int index = r.nextInt(questionNos.size()-1);
    int number = questionNos.remove(index);

那應該可以幫助您達到目標。 您可能需要將列表保存在onPause中,然后將其加載到onResume中,以了解應用停止或旋轉的時間。

如果要生成唯一性,則可以使用System.currentTimeMillis()代替使用number = new Random(); number.nextInt(25); number = new Random(); number.nextInt(25);

例:

//Random m = new Random();
long mQuestionNo = System.currentTimeMillis();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM