Таким образом, я пытаюсь создать игру-викторину, но всякий раз, когда я переворачиваю свой экран, я сталкиваюсь со следующими проблемами: 1. Когда я поворачиваю экран, и он меняется на альбомную, даже если я выбираю неправильный ответ, я получаю неправильное сообщение, но счет все еще увеличивается. 2. Вопрос и выбор меняются, а не остаются неизменными при каждом повороте экрана.
вот мой код:
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class quiz extends AppCompatActivity {
Button answer1, answer2, answer3, answer4;
TextView score,question;
int count=0;
String text="Wrong";
private question mq= new question();
private String manswer;
private int mScore=0;
private int mquestionLength= mq.nquestion.length;
Random r;
private static final String Sscore="";
private static final String nums="";
private String Ques;
private int num;
ArrayList<Integer> list = new ArrayList<Integer>();
private String arr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
num=0;
r = new Random();
answer1 = (Button) findViewById(R.id.answer1);
answer2 = (Button) findViewById(R.id.answer2);
answer3 = (Button) findViewById(R.id.answer3);
answer4 = (Button) findViewById(R.id.answer4);
score = (TextView) findViewById(R.id.score);
question = (TextView) findViewById(R.id.question);
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mquestionLength));
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num++;
if (answer1.getText() == manswer) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mquestionLength));
}
else {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
updateQuestion(r.nextInt(mquestionLength));
}
if(num>8){
GameOver();
}
}
}
);
answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num++;
if (answer2.getText() == manswer) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mquestionLength));
} else {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
updateQuestion(r.nextInt(mquestionLength));
}
if(num>8){
GameOver();
}
}
}
);
answer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num++;
if (answer3.getText() == manswer) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mquestionLength));
} else {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
updateQuestion(r.nextInt(mquestionLength));
}
if(num>8){
GameOver();
}
}
}
);
answer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num++;
if (answer4.getText() == manswer) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mquestionLength));
} else {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
updateQuestion(r.nextInt(mquestionLength));
}
if(num>8){
GameOver();
}
}
}
);
}
private void updateQuestion ( int nextInt){
if(count==0) {
question.setText(mq.getQuestion(nextInt));
answer1.setText(mq.getChoice(nextInt));
answer2.setText(mq.getChoice2(nextInt));
answer3.setText(mq.getChoice3(nextInt));
answer4.setText(mq.getChoice4(nextInt));
manswer = mq.getAnswer(nextInt);
}
else{
if(!list.contains(nextInt)){
question.setText(mq.getQuestion(nextInt));
answer1.setText(mq.getChoice(nextInt));
answer2.setText(mq.getChoice2(nextInt));
answer3.setText(mq.getChoice3(nextInt));
answer4.setText(mq.getChoice4(nextInt));
manswer = mq.getAnswer(nextInt);
}
else{
updateQuestion(r.nextInt(mquestionLength));
}
}
list.add(nextInt);
count++;
}
private void GameOver() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(quiz.this);
alertDialogBuilder.setMessage("Game Over! Your Score is "+mScore).setCancelable(false).setPositiveButton(
"NEW GAME", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(),quiz.class));
}
}).setNegativeButton("EXIT", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog=alertDialogBuilder.create();
alertDialog.show();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(Sscore,mScore);
outState.putInt(nums,num);
outstate.putIntegerArrayList(arr,list);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mScore=savedInstanceState.getInt(Sscore);
num=savedInstanceState.getInt(nums);
score.setText("Score: "+mScore);
list.addAll(savedInstanceState.getIntegerArrayList(arr));
}
}