所以我实现了saveInstanceState方法,但是我不知道我做错了什么。

发生的情况是,旋转屏幕时,所显示的问题成为列表中的第一个问题。但是奇怪的是,当回答问题时,应用程序会跳过一个问题,转到下一个问题。

例如,我在问题6上。屏幕旋转,再次转到问题1,但是如果我要回答问题1,则出现的下一个问题是问题8。每次屏幕旋转都算作一个问题。我糊涂了。

List<Question> mQuestionList;
int score = 0;
int qid = 0;
Question currentQ;
TextView txtQuestion, times, scored;
Button button1, button2, button3, button4;
private static final String TAG = "NoTimerQuestion";
private static final String KEY_INDEX = "index";
private static final String KEY_SCORE = "scoreindex";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    DbHelper db = new DbHelper(this); // my question bank class
    mQuestionList = db.getAllQuestions(); // this will fetch all questions
    currentQ = mQuestionList.get(qid); // the current question
    txtQuestion = (TextView) findViewById(R.id.txtQuestion);
    times = (TextView) findViewById(R.id.timers);
    times.setText("");

    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);
    scored = (TextView) findViewById(R.id.score);

    if(savedInstanceState != null){
        qid = savedInstanceState.getInt(KEY_INDEX,0);
        score = savedInstanceState.getInt(KEY_SCORE,0);
    }

    setQuestionView();

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            getAnswer(button1.getText().toString());
        }
    });
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getAnswer(button2.getText().toString());
        }
    });
    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getAnswer(button3.getText().toString());
        }
    });
    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getAnswer(button4.getText().toString());
        }
    });
}
public void getAnswer(String AnswerString) {
    if (currentQ.getANSWER().equals(AnswerString)) {
        score++;
        scored.setText("Score : " + score);
    }
    if (++qid < 20) {
        currentQ = mQuestionList.get(qid);
        setQuestionView();
    } else {
        Intent intent = new Intent(NoTimerQuestion.this,
                ResultsActivity.class);
        Bundle b = new Bundle();
        b.putInt("score", score); // Your score
        intent.putExtras(b); // Put your score to your next
        startActivity(intent);
        finish();
    }
}

private void setQuestionView() {
    txtQuestion.setText(currentQ.getQUESTION());
    button1.setText(currentQ.getOPTA());
    button2.setText(currentQ.getOPTB());
    button3.setText(currentQ.getOPTC());
    button4.setText(currentQ.getOPTD());
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
    super.onSaveInstanceState(savedInstanceState);
    Log.i(TAG, "onSaveInstanceState");
    savedInstanceState.putInt(KEY_INDEX, qid);
    savedInstanceState.putInt(KEY_SCORE, score);
}
}

最佳答案

在使用qid加载问题之前,您需要还原mQuestionList.get(qid),否则该问题将始终是第一个,因为qid尚未还原。

设置内容视图后直接放置它,例如(在onCreate内部):

setContentView(/*...*/);
if (savedInstanceState != null){
    qid = savedInstanceState.getInt(KEY_INDEX,0);
}
// Now load your question with the qid
currentQ = mQuestionList.get(qid);


另外,为了获得正确的qid,您需要在恢复1时减去qid或仅在实际加载新问题时才将其递增:

public void getAnswer(String AnswerString) {
    // ...
    if (++qid < 20) { // - add increment here
        currentQ = mQuestionList.get(qid);
        setQuestionView();
    } else {
        // ...
    }
}

private void setQuestionView() {
    // ...
    // qid++; - remove increment here
}

10-06 09:00